Hey everyone! I have a question about this code we are fixing and I’m wondering why in this code there are no semicolons used?
const whatRelation = percentSharedDNA => {
if (percentSharedDNA === 100) {
return 'You are likely identical twins.'
}
if (percentSharedDNA > 34) {
return 'You are likely parent and child or full siblings.'
}
if (percentSharedDNA > 13) {
return 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
}
if (percentSharedDNA > 5) {
return 'You are likely 1st cousins.'
}
if (percentSharedDNA > 2) {
return 'You are likely 2nd cousins.'
}
if (percentSharedDNA > 0) {
return 'You are likely 3rd cousins'
}
return 'You are likely not related.'
}
console.log(whatRelation(34))
// Should print 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
console.log(whatRelation(3))
// Should print 'You are likely 2nd cousins.'
From my googling, I understand that
JavaScript does not strictly require semicolons. When there is a place where a semicolon is needed, it adds it behind the scenes. This is called Automatic Semicolon Insertion.
So now I’m a bit stumped as when you must use them?
I also came across this article discussing the use of them after }.
Avoid Semicolons After ‘}’. Do not put a semicolon after a closing curly bracket ‘}‘. The only exception is an assignment statement like this: var data = {name: “Alice”, age: 25};
However, I felt like I’d seen }; being used continuously throughout the ‘Functions’ syllabus:
Helpful Functions
function multiplyByNineFifths(number) {
return number * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};
getFahrenheit(15); // Returns 59
Arrow Functions
const rectangleArea = (width, height) => {
let area = width * height;
return area;
};
Concise Body Arrow Functions
const squareNum = (num) => {
return num * num;
};
So yeah, now I’m a bit stumped