Due to a misplaced semicolon, this code block will execute regardless of the value of x.
if (x == 19); { // code block } |
JavaScript automatically closes a statement at the end of a line by default.
As a result, these two examples will produce the same outcome:
function myFunction(a) { let power = 10 return a * power } |
function myFunction(a) { let power = 10; return a * power; } |
JavaScript also allows you to split a statement across two lines.
Therefore, example 3 will produce the same result as well.
function myFunction(a) { let power = 10; return a * power; } |
But what happens if you split the return statement across two lines like this:
function myFunction(a) { let power = 10; return a * power; } |
The function will return undefined!
Why? Because JavaScript interprets it as:
function myFunction(a) { let power = 10; return; a * power; } |