Line At A Time
Lesson 10: Triple Equals
Triple Equals Compare values the JavaScript way: ===.
1const number = 2;
2if (number === 2) {
3 console.log("It really is 2");
4}
Terminal
$ node main.js
It really is 2
Every line, explained
const number = 2;
const creates a variable: a named box that stores a value. const is short for "constant": once a value is stored, that name can never be given a new value. Modern JavaScript uses const for most things. The semicolon ; marks the end of the statement.
if (number === 2) {
if checks the condition inside its round brackets. If the condition is true, JavaScript runs the code between { and }; if it is false, it skips that code completely. === asks "are these exactly equal?", and it is THE way to compare in JavaScript. A single = stores a value. There is also a sloppy == that converts types before comparing ("2" == 2 is true!), which causes weird bugs, so modern JavaScript always uses === and its partner !==.
console.log("It really is 2");
console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot.
}
This } closes the if block.