No Semicolon Before [] Is Causing Error in JavaScript

No semicolon before [] is causing error in JavaScript

When I'm worried about semicolon insertion, I think about what the lines in question would look like without any whitespace between them. In your case, that would be:

console.log([a,b].length)[a,b].some(function(x){ etc });

Here you're telling the Javascript engine to call console.log with the length of [a,b], then to look at index [a,b] of the result of that call.

console.log returns a string, so your code will attempt to find property b of that string, which is undefined, and the call to undefined.some() fails.

It's interesting to note that str[a,b] will resolve to str[b] assuming str is a string. As Kamil points out, a,b is a valid Javascript expression, and the result of that expression is simply b.

How do I fix this missing semicolon syntax error in Javascript?

Your issue is the fact that the i in function is the unicode character i. If you change it to a 'normal' i it should just work.

But now I'm wondering how the hack :) did you get an unicode character there :P

unicode error in js

Use of semicolons in ES6

Without the semicolon [1,2,3,4,5,6] will be evaluated as property access. Which is perfectly fine JS, I personally don't think that adding semicolons is such a big deal so I keep using them.

Javascript Error : While using array destructuring

You are missing semicolons in your code due to which the compilation is affected

console.log("b ",b)    // b = 2

[a,b] = [b,a]

is treated as

console.log("b ",b)[a,b] = [b,a]    // b = 2

i.e it tries to access a key from console.log return value which isn't defined

Working demo

var list=["Hello", "World"];var [a,b] = list;  console.log("a ",a);    // a = 1  console.log("b ",b);    // b = 2
[a,b] = [b,a];
console.log("a ",a) // a = 2 console.log("b ",b) // b = 1

Why does a multiline comment cause automatic semicolon insertion?

This is expected, it's working as designed. The ES6 specifiction says the following on the grammar for comments:

Comments behave like white space and are discarded except that, if a
MultiLineComment contains a line terminator code point, then the
entire comment is considered to be a LineTerminator for purposes of
parsing by the syntactic grammar.

And this LineTerminator then causes automatic semicolon insertion to kick in, following the usual rules.



Related Topics



Leave a reply



Submit