Search code examples
javascriptsyntax-errortypeerror

Why my code not executing before syntax error causing line in JavaScript


// snippet for syntax error
console.log("hello stackoverflow!");
const test3;
test3='❌'
console.log(test3)

var name="me"
console.log(name)

I understand that JS is synchronous and execute code line by line, but why over here hello stackoverflow! is not getting printed, as the error is in the next line.

Also, Consider another snippet below

// snippet for typeError
console.log('hello stackoverflow!')
const test3 = '✅'
test3='❌'
console.log(test3)

var name="me"
console.log(name)

This snippet prints the `hello stackoverflow`. Which is different behavior from the first snippet.

Can Anyone help me why it's happening and who identifies these errors in JS and does that happen before memory allocation in JavaScript?

I was expecting console.log() to get printed in both scenarios which is not happening in the first snippet.


Solution

  • The order things happen in JS is not strictly linear.

    Syntax errors are scanned for before execution begins.