Proper use of syntax, specifically semicolon in javascript


#1

This is a solution to one of the exercises:

const lifePhase = age => {
if (age < 0 || age > 140) {
return ‘This is not a valid age’
} else if (age < 4) {
return ‘baby’
} else if (age < 13) {
return ‘child’
} else if (age < 20) {
return ‘teen’
} else if (age < 65) {
return ‘adult’
} else {
return ‘senior citizen’
}
}
Here’s a link to the exercise.
https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/life-phase

My question. There are no semicolons ending lines. Should there be a semicolon right before a curly bracket (e.g. … return baby; } … ? Also, since this is a function expression, should there be a semicolon after the last curly bracket?

Thank you.


#2

semi-colons are part of some statements, whether that’s the case for a particular statement is something you can find out by looking up the syntax for that statement, for example on mdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Syntax

Javascript has some “helpful” features, one of them is to make semi-colons automatically inserted if missing. This doesn’t say anything about whether it’s better to include them or omit them, but it’s a terrible choice to make them optional.


#3

On that we may both agree. It might have been simpler for people to learn the correct usage if they were faced with syntax errors at every turn.