LoginSignup
3
3

More than 5 years have passed since last update.

Node.js 4系と6系でfor文のconst変数宣言の動作に気を付けよう

Posted at

まず const で宣言した変数(定数)に再代入しても 4 系 ではエラーにならず無視されることに気を付けましょう。

それを踏まえて、下の例では for..of 文の変数を const で宣言しています。4 系では意図した結果になっていないものの、これで通ってしまいます。

$ node -v
v4.5.0

$ node
> for (const el of [1, 2, 3]) {
... console.log(el);
... }
1
1
1

6 系では問題なく動作します。

$ node -v
v6.6.0

$ node
> for (const el of [1, 2, 3]) {
... console.log(el)
... }
1
2
3

eslint で const 強制を指定したときに指摘されて 6 系では const にできることを知って気づいた Tips でした。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3