Statement Termination in JavaScript

Question
Is it okay to omit semicolons when terminating statements?

Answer
Yes, but it’s not recommended.

Comments
Why? It’s easy to introduce errors when relying on a complete understanding of JavaScript’s automatic semicolon insertion (ASI) mechanism. In many cases, developers assume a semicolon will be inserted when it will not.

Example

// Valid but not recommended.
let name = "Jason"
function sayHello() {
    console.log("Hello " + name)    
}
sayHello()

// Recommended.
let name = "Jason";
function sayHello() {
    console.log("Hello " + name);
}
sayHello();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s