do you need a semicolon in javascript

Do you need a semicolon in javascript?

Yes, you need a semicolon at the end of each statement in JavaScript.

The semicolon is used to separate multiple statements on the same line, and it is also used at the end of each statement to indicate the end of that statement.

If you omit the semicolon, JavaScript will try to automatically add it, but this can sometimes lead to unexpected errors and behavior.

Here is an example of using a semicolon:


    let greeting = "Hello World";
    console.log(greeting);
  

In the above code, the semicolon is used to indicate the end of the "let" statement, and to separate it from the "console.log" statement which follows it.

However, there are some exceptions to this rule. For example, if you are using a "for" loop or an "if" statement, you do not need to use a semicolon after the closing parenthesis. Here is an example:


    for (let i = 0; i < 10; i++) {
      console.log(i);
    }
  

In this case, there is no semicolon needed after the closing parenthesis of the "for" loop.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe