+ 9

Is semi colon necessary at the end of every statement in javascript.

I realized that there is a semicolon at the end of every statement in javascript just like Python

27th Aug 2020, 1:46 PM
Haxsys
Haxsys - avatar
22 Respuestas
+ 22
The rules for semicolon are actually not so hard to get as they really do make sense. You just need to understand the rules for ASI. I put together a simple code that will demonstrate this more clearly. Be sure to read the output of the code and the comments of the code as these tell a story of sorts to help make the point clearer. 😉 Hope this helps!👌 https://code.sololearn.com/WzluRho1k50G/?ref=app
28th Aug 2020, 1:56 AM
David Carroll
David Carroll - avatar
+ 14
Is it me or do people just post answers on questions that seem like low hanging fruit without reviewing whether or not their answer has already been posted several times already? Anyway, I'm unfollowing this thread as it's already been settled. Please mention me by name if you need me to respond. Hopefully people will find the insightful answers buried under all the sound bites that echo the same partial information.
28th Aug 2020, 11:52 PM
David Carroll
David Carroll - avatar
+ 12
Nope, in javascript, there is no need to write a ; at the end unless you are writing two lines of code in the same line, such as, console.log(1); console.log(2) or inside a for loop, for(let i = 0; i < 5; i++){} But otherwise you don't need a semicolon, i personally don't write semicolons, coz it looks cleaner to me, your choice.
27th Aug 2020, 2:04 PM
maf
maf - avatar
+ 12
So, there are multiple opinions about this topic and professionals have been debating about this for a long. JavaScript implemented a feature called "Automatic semicolon insertion". Some people say it's good to have semicolon and some says otherwise. It's all about preferences now, I prefer to put semicolon after every statement, maybe you have different preference.
27th Aug 2020, 2:56 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 9
Pranav Kalro Read my answer carefully, I have mentioned that too :)
27th Aug 2020, 2:04 PM
Ayush Dwivedi
Ayush Dwivedi - avatar
+ 8
Pranav Kalro In python , ";" is used to write multiple statements in a single line . Try this : A=6;print(A) Without ";" , it will give a syntax error. Though using semi colon is optional in python .
27th Aug 2020, 1:51 PM
Ayush Dwivedi
Ayush Dwivedi - avatar
+ 8
In JS it's a bit ambiguous, as Rick said. In Python, however, it is clear cut: You write semicolons ONLY when you add another statement to the line. (According to my taste this means: Never ever.)
27th Aug 2020, 10:40 PM
HonFu
HonFu - avatar
+ 7
Ayush Dwivedi 🇮🇳 Yes, but the semi colon is not necessary if we write all statements seperately.
27th Aug 2020, 1:56 PM
Pranav
Pranav - avatar
+ 6
David Junior [INACTIVE] and David Nkana, checkout the explanation in my earlier response, review the code, and read the article linked in that code. It should give you a clearer picture of things.
28th Aug 2020, 8:15 PM
David Carroll
David Carroll - avatar
+ 5
There is no semi colon after the end of a statement in Python. The semi colon should be there in JavaScript.
27th Aug 2020, 1:49 PM
Pranav
Pranav - avatar
+ 4
Omitting semicolon can cause unexpected results in some later codes. Just put it there and be free.
28th Aug 2020, 10:16 PM
Martin Hanson
Martin Hanson - avatar
+ 4
maf yh it looks cleaner without semicolon
29th Aug 2020, 10:02 AM
Haxsys
Haxsys - avatar
+ 3
No
27th Aug 2020, 1:48 PM
Allamprabhu Hiremath
+ 3
Js reqired semi colon Python required indent
27th Aug 2020, 1:59 PM
Divya Mohan
Divya Mohan - avatar
+ 2
AFAlK Yes, unlike in Python 🤔
29th Aug 2020, 5:55 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Not everywhere
28th Aug 2020, 6:21 AM
Ayush Maurya🇮🇳
Ayush Maurya🇮🇳 - avatar
+ 1
A semi-colon in JS usually shows the end of a block of code I don't think it is really necessary since the code still runs properly without it
28th Aug 2020, 1:49 PM
David Junior
David Junior - avatar
+ 1
Although JavaScript can work without semicolon as code line terminator, I would advise you to use semicolon line terminator as your coding habit, if you are also using C, C#, C++ or Java languages. If you write JavaScript without semicolons ;, you would almost certainly make a lot of "missing ; semicolon" errors, when you switch to Java or C type language. So make it a habit write code with semicolon, would make you write better C or Java languages with less errors.
29th Aug 2020, 2:12 PM
Calviղ
Calviղ - avatar
0
A semi colon indicates where the line of code ends, but it is not that neccessary
28th Aug 2020, 7:33 PM
David Nkana
David Nkana - avatar
0
In python there is no semicolumn at the end if every line. I don’t use it in python. It is optional for python. That column is used in JavaScript to mark the end of the line of code.
29th Aug 2020, 6:51 AM
Mystic
Mystic - avatar