Conditional Expressions
Data Types True or False???
Everything in JS is truthy except for the following six things!
- false (of course)
- The null data type
- The undefined data type
- The empty string ''
- The number 0 (zero)
- NaN (special number)
If it's not in the above list, it's truthy!
The NOT operator
The not operator (!), also known as the "bang" operator, "flips" a true/truthy expression to the boolean value of false, and vice-versa.
For example:
!false === true; // true
!null === true; // true
!3 === false; // true
!'' === true; // true
A double ! operator is a great way to force an expression into its actual boolean value of true or false:
console.log(!!3);
Boolean Logic
Comparison Operators are used to compare the values of left and right operands which can be variables, literal values, object properties, etc.
Operator | Purpose |
---|---|
=== | strict equality - best practice |
== | performs type conversion (coercion) - not recommended |
!== | strict inequality |
!= | inequality |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
OR operator (||)
'hello' || 'goodbye'; // evaluates to 'hello'
0 || null; // evaluates to null
AND operator (&&)
'hello' && 'goodbye'; // evaluates to 'goodbye'
0 && null; // evaluates to 0