JavaScript syntax parser

JavaScript syntax

syntax parser

javascript engine reads each charater of the line-code and anticipates the next character, if it doesn't find the match it syntax parser throws an exception.

auto semicolon insertion

Yes, javascript-engine takes decision on inserting semicolon on need basis. Unlike many programming language semicolon is optional in javascript, instead during syntax parsing if the parser feels there should be a semicolon it inserts by its own. Let's have a look at the below code. function display(){ var a = 1; return // after return statement, syntax parser expecting a semicolon but it got a carriage return! //So it put a simicolon of its own. a; // code has never reached to this line , coz it has returned from above. } console.log(display());
undefined
function display(){ var a = 1; return a;// after return statement, syntax parser expecting a semicolon but it got a space! //So it kept on reading line till it get a semicolon or carriage return. } console.log(display());
1
As you can see the above behaviour has a very grave consequences on situation basis.
Back Next