Mandatory semicolon

How come
let me = function(){} me = 6
needs a semicolon after the closing curly brackets?

Unless its possible for the second me to be passed as a parameter, which it isn’t, I don’t see any other interpretation of this line :cry:

That’s JavaScript. That is 2 lines

Maybe it’s related to rule #2 here: https://www.freecodecamp.org/news/lets-talk-about-semicolons-in-javascript-f1fe08ab4e53/

when the next line starts with a }, closing the current block

It’s after the } here but maybe it has to do with completing the block related to {}.

2 Likes

Yup, its definitely rule 2, but how can omitting the semicolon be ambiguous?

If you don’t have an answer, my thoughts are that () brackets could be inferred in such a way that the part after } is an argument. Probably incorrect though

This should be two separate lines of code.
The first line (let me = function(){}) declares me as an empty function, the parentheses being for function parameters and the curly braces being for the function code itself.

The second line (me = 6) reassigns me as a numerical value (i.e. no longer a function).

If you write it as let me = function(){}; me = 6, it will be interpreted as two separate lines of code, although it would be better simply to put it on two separate lines anyway (and still use semicolons at the end of each line).

I think you’re really going to like Python

Please answer my question :sob:

^ characterlimit-----------

Never mindI’m stupid, its on the same line which is invalid

I would assume it is because it’s a function expression, which is an assignment.

This doesn’t work either.

let me = 6 me = 42

If it was a function declaration it would work.

function me() {} me = 6
console.log(me) // 6

If you comma-separated the assignments you also have to change let to var

let me = function(){}, me = 6
console.log(me) // Uncaught SyntaxError: Identifier 'me' has already been declared

Works

var me = function(){}, me = 6
console.log(me) // 6

I can’t point to the exact part of the specs that are relevant.

https://262.ecma-international.org/10.0/#sec-automatic-semicolon-insertion

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.