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.

23 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.

69 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.

9 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.

2 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

2 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