Closed yurivict closed 5 years ago
Yes. This is as expected. Use semicolons or you will be surprised!
This is what you wrote:
p("--2--")[1,2,3].forEach(function(a){});
p() returns undefined, from which you're taking index 3 (1,2,3 is a comma expression).
Why are semicolon rules different for forEach
?
For example, this works:
print('A')
print('B')
but this doesn't:
print('A') print('B')
SyntaxError: semi.js:2: unexpected token: (identifier) (expected ';')
Based on this example, semicolon is optional when the newline is present. Shouldn't it be the same for all instructions?
Also, see here: https://www.quora.com/Are-semicolons-required-in-JavaScript
Is there a paragraph in the JS standard that specifies such behavior?
The problem is not forEach, the problem is a function call followed by the array constructor.
Javascript generally requires semicolons, but it automatically inserts semicolons before newlines, if the parse would otherwise fail.
Please run the testcase attached below.
When the
p
call is present, it breaks inforEach
in the line 9:Removing the
p
call eliminates the error message.---begin testcase
forEach.js
------end testcase
forEach.js
---