+ 1
What defines boundaries of single statement in JavaScript
In low level languages a statement ends with ';'. But as JS doesn't need semicolon, how some set of code is said to be a statement? Following code inside 'if' control flow is said to be statement because they're surrounded by braces if (condition) { console.log("condition is true") } But what about this: if (condition) console.log("condition is true") How does a pareser know `console.log("condition is true")` is a if statement? Infact what defines it's boundaries?
2 Respostas
+ 3
Good question!
I think in proper terminology, your first example is an if statement followed by a block statement.
In your second example you don't have a block so the if just grabs the next statement.
As you know you *can* put semicolons (;) in javascript but javascript has a mechanism called "automatic semicolon insertion" (ASI) which it uses if you forget putting them in your code. For some reason the community has decided to not use semicolons at all. I don't like the style personally, but ASI should perfectly describe what you are looking for—where one statement ends and another begins.
Here's a good description of the process: https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi
0
Schindlabua thanks for answer. I don't get most of ASI rules.
I'm creating a syntax highlighter that tokenizes given code. In that I want to get statement inside control flows (if-else,try-catch) and iterations (for, while, do) that are not surrounded by braces.
(Assume that program know where to start reading but don't know where to end)