I’m working on the “Accessing Objects Properties with Variable” (https://www.freecodecamp.org/challenges/accessing-objects-properties-with-variables) exercise right now and noticed that there is no semi-colon at the end of the Object’s curly bracket. So far, all of the accessing objects lessons have ended the object with a curly bracket and semi-colon. Why the change?
Semicolons are weird in JS. Sometimes they’re required, sometimes they’re verboten, and sometimes they’re kinda optional.
This runs fine:
const PI = 3.14159
var radius = 10
var circle = { radius: radius, circumference: radius*2*PI }
console.log("radius = " + circle.radius)
console.log("circumference = " + circle.circumference)
And so does this:
const PI = 3.14159;
var radius = 10;
var circle = { radius: radius, circumference: radius*2*PI };
; ;
console.log("radius = " + circle.radius); ; ; ; ;
console.log("circumference = " + circle.circumference);
There are a few boobytraps and definite no-nos, but there is a lot of leeway. I think tend not to put them after curly braces (since that’s a code block) and put them in other places (but there is probably an exception or two that is not coming to mind.) If you put in an extra semicolon, I think it just gets interpreted as an empty line.
If you look around online you’ll find guides like this.
Semicolon automatic insertion possibly. JavaScript engine reads your code character by character and if there isn’t a semicolon where it expects one it will add it as it determines what’s valid syntax.