Skip to main content

Conditional Expressions

Data Types True or False???

Everything in JS is truthy except for the following six things!

  1. false (of course)
  2. The null data type
  3. The undefined data type
  4. The empty string ''
  5. The number 0 (zero)
  6. 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.

OperatorPurpose
===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