Version: v7.9.0
Platform: Darwin green.local 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64
so i have this in a file
console.log('hello')
const a = 2
[3,4].forEach(n => console.log(n))
and when doing node weird.js i get
[3,4].forEach(n => console.log(n))
^
TypeError: Cannot read property 'forEach' of undefined
at Object.<anonymous> (/Users/pvinis/Desktop/weird.js:3:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
at bootstrap_node.js:538:3
also https://es6console.com/j2uho344/ this might be helpful.
i saw that Array(3,4).forEach.. works fine.
is that a bug? is that on purpose like that?
is that on purpose like that?
It鈥檚 part of the language, yes. Your code is parsed as const a = 2[3,4].forEach(n => console.log(n)), which fails because 2 has no property 4 and so 2[3,4] returns undefined.
You can fix this by either using semicolons consistently, or add one whenever you start a line with an opening bracket or parenthesis.
The question has been answered so I will go ahead and close this.
Most helpful comment
It鈥檚 part of the language, yes. Your code is parsed as
const a = 2[3,4].forEach(n => console.log(n)), which fails because2has no property4and so2[3,4]returnsundefined.You can fix this by either using semicolons consistently, or add one whenever you start a line with an opening bracket or parenthesis.