var name; // declare but do not initialize var name = value; // declare and initialize let name [= value]; // block-scoped variable declaration const name = value; // block-scoped constant
// Note: const can't be reassigned or redeclared
var a = []; a.push( value ) a[i] a.sort() // Only applies to arrays, not strings for ( i in a ) { v = a[i]; ... } var b = a.slice()
var h = {}; h.key = "value" or h["key"] = "value" h.key or h["key"] h.hasOwnProperty( "key" ) for ( k in h ) { v = h[k]; ... } var s = "foo"; s.length == 3 var s = "foo";s[0] == "f"; // faster, but doesn't work on IE7s.charAt(0) == "f"; // slower, but universally supported
var s = "foo"; s.indexOf('o') == 1; // 0-based indexing s.indexOf('x') == -1; // -1 for not found
s.replace(/X/g, 'Y' ) var s = "foo"; // intent: a = [ 'f', 'o', 'o' ] var a = [...s]; // option 1 - spread operator var a = s.split(''); // option 2 - split function
var s = a.join(''); x += 1 or x -= 1 typeof "foo" === "string" and typeof 42 === "number" and typeof {foo:"bar"} === "object" and typeof [1,2,3] === "object"