Dynamic typing, so you don't need to specify.
One number type - 64-bit double, so no need to worry about ints and floats.
3 special not-a-real-number results are Infinity (1/0), -Infinity (-1/0), and NaN (0/0)
Boolean type - true/false
Negation uses !
false, null, undefined, NaN, 0, and "" are falsy; everything else is truthy
Strings - use '
or "
Concatenate with +
, which also automatically converts other types
Access characters with string.charAt()
and substrings with string.substring(0, 5)
Length is a property of a string, so doesn't require brackets string.length
Comparisons
==
performs type coercion, so "5" == 5 //true
===
does not, so "5" === 5 // false
Declare with var
Not required, but otherwise the variable will be declared in the global scope
Set to undefined
if not assigned
Arrays are ordered lists of any type, and work as expected
var myArray = ["Hello", 45, true];
Get length with myArray.length
Add/remove with perl-style keywords (push, pop, ...), slice, splice, ...
Join with myArray.join(",")
Objects are the equivalent of dictionaries/maps
var myObj = {key1: "Hello", key2: "World"};
Keys are strings, but quotes aren't required if they're a valid javascript identifier (aka variable name)
Attributes accessed using subscript syntax myObj["myKey"]
or dot syntax myObj.myKey
(if a valid identifier)
Values that are not yet set give undefined
&&
and ||
short-circuit, so you can set default values
var name = otherName || "default"
Destructuring assignment is an interesting one - lets you easily declare variables directly from object keys
var obj = { a: 123, b: 456, c: 789 };
var { a, c } = obj; // a = 123, c = 789
If works as you'd expect
if
, else if
, else
Same with &&
, ||
So does while (true) { ... }
Also have do { ... } while ()
loops