Semi-colon


#1

Here is my situation, on the project course “Race Day” below is my code.
When I used the semi-colon, it causes problems, so what exactly the right time to use the semi-colon, and what is it used for? is it really important?

/home/ccuser/workspace/learn-javascript-U2P2/main.js:10
else if(!Early && Age > 18);
^^^^
SyntaxError: Unexpected token else
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

let raceNumber = Math.floor(Math.random() * 1000);
let Early = true;
let Age = 18;

if(Early && Age > 18){raceNumber += 1000};
//
if(Early && Age > 18);
{console.log(`No.${raceNumber},Race will begin at 9:30.`)};
//
else if(!Early && Age > 18);
{console.log(`Race will begin at 11:00, your race number is: ${raceNumber}.`)};

else if(Age < 18){console.log(`Race will begin at 12:30, your race number is: ${raceNumber}.`)};

else{console.log('Please to see the registration desk')};

#2

this takes time to learn, after declaring variables and calling functions you should use semi-colons.

semi-colons end statements, using the semi-colons in if/else if/else clauses will cause it to work incorrectly.

yes, if you don’t insert semi-colons, javascript will do it. This can cause absurd corner cases:

const a = 5
const b = 10
const c = a + b

[1, 2, 3].forEach((e) => console.log(e))

that will produce an error, while:

const a = 5;
const b = 10;
const c = a + b;

[1, 2, 3].forEach((e) => console.log(e));

works fine.


#3

The logic ends at the semi-colon.


#4

yes, i covered that in the other topic this learner made :wink: This question is more about the how and the why?


#5

I’d say that pretty much sums it up. End-of-statement equals end-of-logic and the parsed code can now be interpreted.

@linhong-chih76970187: Semi-colons separate imperative statements. They never follow a conditional in an if or while statement since parsing stops there and the associatiate code block executes independent of the conditional.

{
    console.log('this line is inside an anonymous code block.');
}

When we place a semi-colon after the conditional, the code block that follows becomes anonymous and uncontrolled. Strange things can happen.

if (condition) {
    // conditional code
} else if (state) {
    // state code
} else {
    // default code
}

The statements inside the code blocks are usually imperative unless they are for loops or if/while statements. So semi-colons are used in that code, but not on the structure itself.

Function expressions are another example of imperative code since they are assignments.

var foo = function () {
    // code block
};

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.