1

So I run the same code 5=6 on both chrome and node this of course produces an error. But look at the error type!

different errors on chrome vs node

On chrome it's a SyntaxError on node ReferenceError.

Well that's odd. For one thing cause both use v8, for another because well...you'd think this would be defined in the spec, so someone has to be wrong.

The same thing happens with strings. Same thing when running js outside of the chrome console as a script. Same thing when running outside of the node console as a script.

Someone suggested that it might be due to differences in boxing since Number(5) = 6 is a ReferenceError on both but that's just the case for any fn() = _. And if that is the case, then it still doesn't explain why the behavior is different in the two engines.

So what's the correct (as per the spec) thing to do here? Who is wrong and why are the two engines giving me different errors?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 1
    Please correct this sentence: `On chrome it's a SyntaxError on chrome ReferenceError`. Which is which? – jfriend00 Jun 04 '20 at 21:36
  • 1
    When I run `5=6` in an actual JS file in node.js or an actual script file in Chrome (not using command line simulators), I get the exact same error: `Uncaught SyntaxError: Invalid left-hand side in assignment`. So, it appears to me that V8 is doing the same thing either way when given a regular script file to process. So, perhaps any difference has to do with the environment that you're typing that line of code in and how it separately evaluates that expression (which isn't actually part of V8 itself or the JS spec). – jfriend00 Jun 04 '20 at 21:38
  • 1
    The console environment is somewhat weird, it's a good idea to keep that in mind. – Pointy Jun 04 '20 at 21:40
  • As I said in the question @jfriend00 and @Pointy I tried this with a script as well https://imgur.com/a/2HJXaUS you can see its a `ReferenceError`. Fixed that typo btw – George Mauer Jun 04 '20 at 21:46
  • I just tried in Chrome 83 and I definitely get a SyntaxError, not a ReferenceError. – Pointy Jun 04 '20 at 21:48
  • 1
    And, in node v14.3.0, I definitely get a `SyntaxError`, not a `ReferenceError`. So, I cannot reproduce what you describe when using a regular script file in the two environments (not using a repl or debugger). – jfriend00 Jun 04 '20 at 21:49
  • Same for Node 13.6 – Pointy Jun 04 '20 at 21:50
  • @Pointy yes, exactly what I said above and also what. my screenshot shows. `SyntaxError` in chrome, `ReferenceError` in node. Again, its not just a console issue, here it is in a script via fiddle https://jsfiddle.net/8Lwdcmk9/ (open the beta console to see its a `SytnaxError`) – George Mauer Jun 04 '20 at 21:50
  • Well the fact is that two other people are *not* sharing your experience so far. Your Node version is kind-of old too. – Pointy Jun 04 '20 at 21:51
  • Ok, so sounds like node version changes this behavior sometime between 10.15 and 13.6. So that (likely) answers the question that chrome is right and node is wrong. That leaves the question of what does the spec say to do here. – George Mauer Jun 04 '20 at 21:52

2 Answers2

4

The ECMAScript language specification changed in June 2019 so that many things that were previously a ReferenceError (including attempting to assign to a number literal) are now a SyntaxError.

v8 implemented this change (also in June 2019) in version 7.7.196, which was included in Chrome 77 and Node 12.11. Prior to those versions, attempting to assign to a number literal resulted in a ReferenceError in both Node and Chrome. Starting with those versions, the same code now results in a SyntaxError.

$ ./node-v12.10.0 -e 5=6 2>&1 | grep Error
ReferenceError: Invalid left-hand side in assignment
$ ./node-v12.11.0 -e 5=6 2>&1 | grep Error
SyntaxError: Invalid left-hand side in assignment
matte
  • 333
  • 1
  • 5
1

From searching

Assigning to literal is type of syntax error.

A ReferenceError occurs when there was an unexpected assignment somewhere

A SyntaxError occurs when object represents an error when trying to interpret syntactically invalid code

try {
  Number(5) = 6
} catch (e) {
  console.log(e instanceof ReferenceError)  // true
  console.log(e.message)                    // ""
  console.log(e.name)                       // "ReferenceError"
  console.log(e.fileName)                   
  console.log(e.lineNumber)                 
  console.log(e.columnNumber)               
  console.log(e.stack)                      
}

but typeof Number(5) is number but assigning to function is valid in js.

when i write below it give SyntaxError rather than ReferenceError

try {
  5 = 6
} catch (e) {
  console.log(e instanceof ReferenceError)  
  console.log(e.message)                    
  console.log(e.name)                       
  console.log(e.fileName)                   
  console.log(e.lineNumber)                 
  console.log(e.columnNumber)               
  console.log(e.stack)                      
}

Their error details are same but Number(5) in js syntactically valid code , but its error in both of situation are same(Invalid left-hand side in assignment). Only differance is Number(5) is syntactically valid code in js but not in node.

But in node 12.14.0 that I used errors are same with js then, node changed its error type. enter image description here This means js is correct for this error type

mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54