|
|
Javascript Cheat Sheet
- arithmetic:
+ - * / %←means
remainder. There's no ^ for exponentiation!
- math functions:
Math.sin( ), Math.cos( ), Math.log( ), Math.exp( ),
Math.sqrt( ), Math.floor( )
- math constants: Math.PI, Math.E
- variables: define like
var x;
or var x = 0;. Assign like x = x + 1;
- semicolon: use ; to terminate statements,
e.g.
var x; x = 17;
- string literals:
"hi"
or 'hi' or 'I said "hi", right?'
- escaping: within a string literal,
\" for ", \'
for ', \\ for \, \n for newline
- string operations:
"a" + "b"
→ ab, "foobar"[3] → b,
"foobar".length → 6
- strings and ASCII:
- typeof: returns type name as a string, e.g.
typeof(43*7) → Number
- conversions:
String-to-Number
Number("42"), Number-to-String String(42)
- alert: display pop-up window with text,
e.g.
alert("Hello World!");
- prompt: get string from user via pop-up window, e.g
var n = prompt("Enter your name");
|