+ 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
22 Answers
+ 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
+ 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.
+ 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.
+ 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.
+ 9
Pranav Kalro Read my answer carefully, I have mentioned that too :)
+ 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 .
+ 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.)
+ 7
Ayush Dwivedi 🇮🇳 Yes, but the semi colon is not necessary if we write all statements seperately.
+ 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.
+ 5
There is no semi colon after the end of a statement in Python.
The semi colon should be there in JavaScript.
+ 4
Omitting semicolon can cause unexpected results in some later codes. Just put it there and be free.
+ 4
maf yh it looks cleaner without semicolon
+ 3
No
+ 3
Js reqired semi colon
Python required indent
+ 2
AFAlK Yes, unlike in Python 🤔
+ 1
Not everywhere
+ 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
+ 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.
0
A semi colon indicates where the line of code ends, but it is not that neccessary
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.