Code Challenges: JavaScript Fundamentals

Folks, general question regarding the JS function challenges.

When I review the solution code (only after giving the exercise a good go), I notice it marks any code I use a semi-colon as incorrect for example

return earthWeight;

Is marked as not correct, the solution code does not use the semi-colon.

return earthWeight

Why is this happening? I remember one of the earlier JS lessons states that semi-colon’s are not strictly necessary but it’s good practice to always end lines of code with one.

The legacy syntax of JavaScript follows along the tradition of precursor languages like Java, C, Perl, PHP, etc. in the use of semi-colon to indicate to the interpreter/compiler, end of statement delimiters. It serves as a signal to the parsing engine the stop parsing and begin interpreting/compiling the contents of its line buffer.

A lot has changed over the decades as the language has grown and evolved into its present form. One such change is ‘auto-insertion’ of semi-colons at the end of lines (on the assumption it is a complete statement). It is at this point where syntax errors are caught and flagged.

JavaScript has never been as strict as compiled languages and it is easy to ‘forget’ to include the end-of-statement token at each newline. Best practice guidelines have always reminded us of the importance of the token, but not been able to enforce it since JS is script, not compiled execution code. This is even more so now that JS has matured and auto-insertion is de facto and more or less moot.

Still, what’s not to gain by following best practice guidelines? Habits are easy to form and hard to break so best we start with good habits. One thing that has never changed in JS is the last line of a code block where no semi-colon is needed or expected, such as the return line of a function. The next token to appear is the closing curly brace which also serves as a signal to stop parsing.

Bottom line, semi-colon line termination is a good habit even if the language is relaxed enough and durable enough to parse and interpret with or without it. Linters will often badger us into forming good habits, and humoring them can only improve the quality of our code and the formation of good habits.

4 Likes

It isn’t being marked as incorrect. It is just being highlighted as being different than the Codecademy solution. The “View Solution” option from the drop-down menus in the exercises compares your solution to the Codecademy solution and highlights any differences. Your solution won’t be rejected for minor differences. If your solution isn’t being accepted, there must be other reasons (such as incorrect logic, strings not matching specifications such as punctuation or uppercase/lowercase exactly etc.)

1 Like

Thanks mtf, I think this was the point I was trying to make e.g., forming good habits.

I knew my code was correct so was really just questioning why Codecademy wouldn’t do the same with their code. Just seems strange they wouldn’t be reinforcing these good habits with their code but as you mention with auto-insertion it isn’t strictly necessary.

Would you say that it’s good practice to use semi-colon’s at the end of a code block e.g,

{
  code.....
};

Or as you say last curly brace indicates end of code block so semi-colon is not necessary?

Thanks

Mark

1 Like

Gonna cop out and leave it to ‘auto-insertion’ to decide. There was a time when a function expression would be given one, and a function declaration, not. In modern terms, it would probably look foreign to other readers.

3 Likes

Thanks mtrtmk, yes you’re right I was wrong to say it was being marked as incorrect.

As I said in my reply to mtf, I knew my code was correct because it gave consistent results when tested.

My point was one around consistency e.g., we’re taught in previous lessons it is best practice using semi-colons to mark the end of a line of code but their example code doesn’t.

mtf has explained auto-insertion which makes sense.

I’ll stick with using semi-colons as it fits my ageing brain to form good habits early :laughing:

Thanks

Mark

1 Like

Just a closing comment with regard to JS ‘script’. In a compiled language, errant code (syntax, types, etc.) is flagged when we go to compile it. JavaScript will try to run the code, balls to the walls to get to the finish line. That is the difference between script and executables. Script is always in a pre-interpreted, precompiled state and gets read and executed immediately, however it compiles. Without access to the console log record, a web page user would not be aware that any error occurred.

Bottom line, it is important to note this peculiarity about scripts and set one’s expectations around that, as opposed to compiled code execution (C++, C#, Java, et al).

1 Like