JavaScript Cheat Sheet
JavaScript is a a loosely typed and dynamic scripting language developed for the Internet,
and a core technology of the the World Wide Web.
Variables
Variable names can contain uppercase or lowercase letters (Aa to Zz), or numbers (0 to 9),
or an underscore (_). They cannot start with a number.
Data type Global scope Block scope
number
var foo = 10; let foo = 10;
string
var foo = "example"; let foo = "example";
Boolean
var foo = true;
var foo = false;
let foo = true;
Binary operators Assignment shortcuts
a & b Bitwise AND (1 if both bits are 1) a += b;
Addition
a = a + b;
a | b Bitwise OR (1 if either bits are 1) a -= b;
Subtraction
a = a - b;
a ^ b Bitwise XOR (1 if bits differ) a *= b;
Multiplication
a = a * b;
a<<n Shift bits to the left a /= b;
Division
a = a / b;
a>>n Shift bits to the right a %= b;
Modulo
a = a % b;
Interacting with HTML
Set contents of any HTML element with ID
demo to the contents of variable foo
document.getElementById("demo").i
nnerHTML = foo;
Set contents of button element to the result
of the Date() function
<button onclick="this.innerHTML =
Date()">Timestamp</button>
Common HTML events
click drag / dragover / dragend drop
focus keydown / keypress / keyup input
load mousedown / mouseover / mouseup submit
Seth Kenlon CC BY-SA 4.0 Opensource.com