Skip to content
the poor coder

do you need a semicolon in javascript

Poor Man
Published
Reading time
1 min read

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:

Text

    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:

Text

    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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC