Operations (+, -, <, >, and more)
Files in this demo
├─ index.html └─ script.js
Maths
+ - * / do what you’d expect. % is the remainder ~ 7 % 3 is 1. It’s not a percentage, and it’s how you check if a number is even: n % 2 === 0.
Text
+ glues strings together, which means "5" + 5 is "55". Fun.
Better: a template literal, written with backticks:
`You have ${cats} cats`
${...} drops a value straight in, and you can put maths inside it too. I use these for basically all string building.
Comparing
>, <, >=, <= give you true or false.
For equality, use === and !==, with three characters.
The two-character == converts types before comparing, so 5 == "5" is true, "" == 0 is true, and a handful of other results that have caused real bugs in real software. === compares the value and the type, which is what you meant.
Combining
&&~ and. Both must be true.||~ or. Either will do.!~ not. Flips it.
Shorthand
n += 5 is n = n + 5. n++ adds one. There’s -=, *= and -- too.
Click to resume
Use the console below.