Skip to main content TBSandell

Data Types

Primitives

number
While many languages have multiple types for representing numbers (integers, single precision, double precision, long integers, etc.), all Javascript numbers are double precision floating point numbers.
string
Javascript strings are similar to other languages, they can be created with single quotes, double quotes, or backticks
boolean
True or False
array
A list of values, Started and ended with brackets, and delimited with commas
object
A list of key value pairs. Started and ended with braces, delimited with commas, and with colons separating keys from values
/* number */
5
24.7
2.7e5
0b0010 // 2
0o0010 // 8
0x0010 // 16

/* string */
"I'm a string"
'I have "double quotes"'
`I'm a string with "double quotes"
and multiple lines`

/* boolean */
true
false

/* array */
[]
[ 5 ]
[
  24,
  25,
  26, // arrays can have trailing commas
]
[ [5], {"yay": 5}, null, undefined, 6, "no", true ] // arrays can contain any type of value

/* object */
{}
{ "key": "value" }
{ 5: 6 } // keys can be numbers or strings
{ 5: { 6: 7 } } // values can be anything

Operators

Arithmetic

+
Addition
-
Subtraction
/
Division
*
Multiplication
**
Exponentiation
%
Modulus (Division Remainder)
 4  +  3     // 7
"4" + "3"    // "43"
 4  -  3     // 1
 4  /  3     // 1.33333...
 4  *  3     // 12
 4  ** 3     // 64
 4  %  3     // 1

Assignment

=
Assigns a value to a variable
+=
Shorthand for x = x + y
-=
Shorthand for x = x - y
*=
Shorthand for x = x * y
/=
Shorthand for x = x / y
%=
Shorthand for x = x % y
**=
Shorthand for x = x ** y
++
Shorthand for x = x + 1
--
Shorthand for x = x - 1
x =   5    // x:   ? ->  5
x +=  5    // x:   5 -> 10
x -=  5    // x:  10 ->  5
x *=  5    // x:   5 -> 25
x /=  5    // x:  25 ->  5 
x %=  3    // x:   5 ->  2 
x **= 3    // x:   2 ->  8
x++        // x:   8 ->  9
x--        // x:   9 ->  8

Comparison

==
Equal value
===
Equal value and type
!=
Not equal value
!==
Not equal value or type
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
?
Used in conjunction with a colon. If the first value is true, then the expression is equivalent to the second value. Otherwise the expression is equivalent to the third value.

"5" ==  5           // true
"5" !=  4           // true
 5  === 5           // true
"5" !== 5           // true
 5  >   4           // true
 4  <   5           // true
 5  >=  5           // true
 5  <=  5           // true
 5  ==  5 ? 4 : 3   // 4

Logic

&&
And
||
Or
!
Not

true  && true    // true
true  || false   // true
!false           // true
true  && false   // false
false || false   // false
!true            // false

Bit Manipulation

&
And
|
Or
^
Xor
~
Not
<<
Left Shift
>>
Right Shift
>>>
Unsigned Right Shift

0b0110  &  0b0011  // 0b0010
0b0110  |  0b0011  // 0b0111
0b0110  ^  0b0011  // 0b0101
        ~  0b0011  // 0b1100
0b0110 <<  1       // 0b1100
0b0110 >>  1       // 0b0011