Comments
One line |
|
Multi lines |
/* A multi line comment */
|
Data Types
|
3, 3.1, -3.1, 3e5, 3e-5 |
|
"abc", 'abc', "", '' |
|
true, false |
|
No value |
|
Value of nothing |
|
Not a number |
Numbers
|
Declare a number |
|
To string, fixed decimals |
|
Convert a string to int |
|
Convert a string to float |
Strings
|
Declare and assign a string |
|
Length of string |
|
Access a character at index |
|
Index of a substring |
|
Part of string (start, end) |
Arithmetic Operators
|
Add, subtract, multiply, divide |
|
Modulus (reminder) |
|
Assignment |
|
Increment, decrement |
|
Operation and assignment |
|
|
Comparisons
|
Equal to, not equal |
|
with type |
|
Greater/Less or equal |
|
And, Or, Not |
Functions
Declare a function |
function myFunc(p1, p2) { statements; return p1 + p2; }
|
Use a function |
|
Function variable |
var f = function(p1, p2) { statements; return p1 + p2; };
|
Use function varialbe |
|
|
|
Conditionals
if |
if (x > y) { statements; }
|
if..else |
if (x > y) { statements; } else { statements; }
|
if..else if |
if (x > y) { statements; } else if (x > z) { statements; } else { statements; }
|
switch..case |
switch(x) { case 1: statements; break; case 2: statements; break; default: statements; }
|
Loops
|
var x = 0; while (x < y) { statements; ++x; }
|
|
for (var x = 0; x < y; ++x) { statements; }
|
|
Stop current iteration, continue with next one |
|
Stop loop immediately |
|
var x = [ "abc", "efg", "hij" ]; for (i in x) { statements; // i = 0,1,2... }
|
|
var x = [ "abc", "efg", "hij" ]; for (i of x) { statements; // i = "abc","efg",.. }
|
Arrays
|
Declare and assign |
|
Declare empty array |
|
Extract the first element |
|
Set the first element |
|
Length of array |
|
Add element to array |
|
Remove the last element |
a.splice( s, r, e1, e2..)
|
From index s
, remove r
elements, then add e1, e2...
|
|
Run function f(e) for each element in array |
Objects
Literal object |
var x = { name: "cool", size: 10, sayHi = function() { console.log(this.name); } }
|
Constructor |
function MyObj(nm) { this.name = nm; this.size = 10; this.sayHi = function() { console.log(this.name); } }
|
Create new object |
var x = new MyObj("cool");
|
Use object |
console.log(x.size); x.sayHi();
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets