Do I need to use a semicolon?

Hi, would it be possible if you could explain to me if console.log(35) is the same as console.log(35);? Because when I run the two codes, the outcome is the same.
Thank you!

2 Likes

It’s mentioned in lesson 2 that Javascript can run even if you don’t put a semicolon after a function. Though most other languages require a semicolon after completing a function so it’s better to have a habit of putting the semicolon from the start to crub bad habits before they develop, or at least that’s what I understood.

40 Likes

When it comes to entering commands on the console, we don’t ever need to include the semi-colon (end of statement token) since we follow the line with a hard return. The command is executed immediately, unlike a statement which won’t execute until the code is Run.

Entering code at the prompt is a command; writing code that will be run later is a statement.

The reason for having semi-colons at the end of our statements is to signal the interpreter to stop parsing and begin to interpret/compile the code just read in. JS is a script so is compiled as each line is encountered.

Technically, when each statement is written on its own line, semi-colons are not needed since the parser knows where the statement ends and will stop at the end of the line. It’s when all the line-breaks are removed that problems surface. If one’s code is destined to be minified for production use, then forming the habit of always using semi-colons will ensure that process doesn’t corrupt the program.

95 Likes

I think in most cases it should work fine.
However, if you do not put a semicolon, JS has to guess where your statement ends. While this works most of the time, there is chance for a wrong guess and as such an error in your program.

Also, I consider it to be part of a clean and readable code policy as well as of correct programming.

12 Likes

The semicolon denotes the end of the line, or statement. Although in JavaScript your code will usually run as intended without a semicolon, Codecademy recommends learning the habit of ending each statement with a semicolon so you never leave one out in the few instances when they are required.

4 Likes

Sorry, but i didn’t quite understand what you mean by “Parse”. Does it mean to understand the written statements before running it? And what makes the ‘Script’ in “JavaScript” have a different meaning than Java, which is another programming language.

https://www.google.com/search?q=parse

As to the similarity in name, JavaScript is vaguely Java-like, but not Java. Script relies upon a client runtime environment that is session based, such as a web browser. The interpreter that parses the script is a program likely written in a derivative language of C. It is that program that compiles each line of code (statement) in real time as the script is run, top down. Some or much of the code will only run one time. To get it to run again we need to refresh the page.

Bottom line, script is not pre-compiled the way Java and C are. They are not standalone programs

3 Likes

This is incorrect: the interpreter won’t interpret a return or linefeed as an end of a statement; this is what the semicolon is for. It usually figures out where each statement begins and end but not always. My most recent incursion:

if (array[i] < array[i - 1]) {
	isUnsorted = true; // no semicolon here assigns following tuple to isUnsorted
	[array[i], array[i - 1]] = [array[i - 1], array[i]]
}

Additionally, this also runs fine:

let
  a
    =
    1

let
  b
    =
    2

console
  .log
  (a
    +
    b)
1 Like

i dont know why can someone please tell me?

I wrote following code

  1. console.log(26)
    2)console.log(1);
    and it prints: 26
    1
    that means semicolon is not compulsory,am i right or wrong.

The semicolon is not always necessary and in this case the code will run fine without it, however it is best practice to include a semicolon after each line in case we encounter any issues when the computer is running the code

Not compulsory, as in leniently allowed to be absent, yes. Necessary? In many cases, no, unless we wish to compress (minify) our code which puts all the code on one line. If statements are not concluded with a semi-colon then when that code runs the interpreter won’t know where one statement ends and the next begins.

Any decent minifier program will know where to insert the needed semi-colons to prevent that sort of thing, and JS itself may even step in and automatically insert any it sees are missing. This is what we mean by lenient. Take a couple of examples…

const addab = (a, b) => a + b
const subab = (a, b) => b - a

Now remove the line break and let the second function slam up against the end of the first…

const addab = (a, b) => a + bconst subab = (a, b) => b - a

We can see where there would an issue. This would have to throw an error if it even makes it into runtime mode; otherwise the parser will raise a syntax error when it tries to parse out keywords and variables.

This will not throw any errors…

const addab = (a, b) => a + b;const subab = (a, b) => b - a

Is that making any sense?

2 Likes

A post was split to a new topic: JavaScript will try its best to figure out what you meant