DEV Community

stalin s
stalin s

Posted on

Is SemiColon in javascript optional??

YESSSSS.

JavaScript inserts semicolons for you automatically. That’s why it’s optional to write semicolons. You don’t need semicolons to write good JavaScript.

In fact, it’s easier to read and write JavaScript when you leave out the semicolons. Many experts have already done so. I’d leave the explaining of why you don’t need semicolons to this

Image description

Happy Learning

Top comments (2)

Collapse
 
alexmustiere profile image
Alex Mustiere

Suppose you wanted to write

MyClass.prototype.myMethod = function() {
    return 42;
};
(() => {
})();
Enter fullscreen mode Exit fullscreen mode

But write instead this:

MyClass.prototype.myMethod = function() {
    return 42;
}
(() => {
})();
Enter fullscreen mode Exit fullscreen mode

BIG difference!

My recommandation: you, as a developer, know where your statement ends, so use a semicolon to mark this end, don't let the JS engine decide for you.

Collapse
 
revenity profile image
Revenity

I still prefer writing semicolons
Maybe I'm more familiar with Java so I think the code looks more clear
And that's why I don't like Python
¯_(ツ)_/¯