let
-
no data type required because JS is a loosely-typed language
let x;
x = 5; // number
x = "cool"; // string
x = true; // boolean
true
or false
document
, and you
can create your own
const PI = 3.142;
// Single line
/* Multi-
line */
+ - * / %
+
which also performs string concatenation
x = 2 + 3; // 5
x = 2 + "3"; // "23"
x = "2" + "3"; // "23"
parseInt()
or parseFloat()
to convert a string
into a number
x = 2 + parseInt("3"); // 5
x = parseFloat("2.4"); // 2.4
x = parseInt("pig"); // NaN (Not a Number)
+= -= *= /= %=
x = 2;
x += 3; // 2 + 3 = 5
x %= 2; // 5 % 2 = 1
++ --
x = 2;
x++; // 3
== != < <= > >=
10 > 9 // true
"10" > 9 // true (LHS converted to number first)
"10" > "9" // false (charCode of 1 < charCode of 9)
=== !==
for comparing the value and type
x = 5;
x == 5; // true
x == "5"; // true
x === 5; // true
x === "5"; // false (x is a number, not a string)
// All evaluate to true
if (true)
if (123)
if ("blah")
// All evaluate to false
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ("")
&& ||
// Evaluates to true if both simple expressions are true
if (x < 20 && y != 2)
!
// Using the not operator is generally not preferred
if (!(x < 20))
// simplfied to
if (x >= 20)