- String
- Number
- Boolean
- Undefined
- Null
Variables can't start w/ numbers or special characters except _
and $
.
var year; // initialized w/o value (undefined)
var year = 2011; // initialized and set (defined)
- var is not required when setting variables but is a best practice.
- variables are case-sensitive
var year, month, day;
var year=2011, month=10, day=31;
- Equal
==
or===
==
does type coercion (conversion) before checking for equality; and ===
does strict equation which requires values to have the same type as well.
0 == false // true
0 === false // false, because they have different types (int, bool)
1 == "1" // true
1 === "1" // false, because they have different types (int, string)
null == undefined // true
null === undefined // false
-
Or
||
-
And
&&
-
Not
!
-
GT
>
-
LT
<
-
Modulo
%
returns the remainder of one number divided by another. e.g.,14 % 3 === 2
It's useful to test divisibility. The result of 0 indicates perfect divisibility.
Ternary operators are used as conditional shortcuts. The syntax is:
condition ? result1 : result2;
The following examples are equivalent.
result = x > y ? 'good job' : 20;
if (x > y) {
result = 'good job';
} else {
result = 20;
}