Open original
Info
Demo 3.3

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.

Next demoConditional logic using <code>if...then</code> statements
Keyboard Shortcuts
⌘ Z / ⌘ ⇧ ZCtrl Z / Ctrl ⇧ Z Undo / redo
Tab / ⇧ TabTab / ⇧ Tab Indent / unindent selection
⌘ ] / ⌘ [Ctrl ] / Ctrl [ Indent / unindent lines
⌘ ACtrl A Select all
⌘ DCtrl D Select next occurrence of selection
⌘ /Ctrl / Comment / uncomment lines
Ctrl SpaceCtrl Space Autocomplete
Ctrl EnterCtrl Enter Reset layout
⌘ ⌥ [ / ⌘ ⌥ ]Ctrl ⇧ [ / Ctrl ⇧ ] Fold / unfold block
Code
The editor didn't load — try refreshing the page. Navigation, shortcuts and the info panel still work in the meantime.
Preview
Dimensions0 × 0
Delay
Console