Truthy and Falsy values, Null Vs Undefined, double equal (==) vs triple equal (===)

Mahbubur Rahman
2 min readJan 14, 2022

Truthy and Falsy values:

As well as a type, each value also has an inherent Boolean value, generally known as either truthy or falsy. Some of the rules are a little bizarre, so understanding the concepts and effect on comparison helps when debugging JavaScript applications.

The following values are always false:

  • false
  • 0 (zero)
  • -0 (minus zero)
  • 0n (BigInt zero)
  • ‘’, “”, `` (empty string)
  • null
  • undefined
  • NaN

Everything else is truthy. That includes:

  • ‘0’ (a string containing a single zero)
  • ‘false’ (a string containing the text “false”)
  • [] (an empty array)
  • {} (an empty object)
  • function(){} (an “empty” function)

Null Vs Undefined:

Null

const dogA = {

‘size’: 3,

‘owner’: ‘Bob’ // has the owner ‘Bob’

};

const dogB = {

‘size’: 3,

‘owner’: null // has no owner

};

The field owner is in both cases obviously defined. Therefore, it won’t return undefined. If the owner is set, it will return the owner’s name, if not, it will return null. The value null means something like The accessed property exists but there is no value set.

Undefined

const dogA = {

‘size’: 3,

‘owner’: ‘Bob’

};

const result = dogA.age; // result will be ‘undefined’

In this example, we try to access the property age of dogA, even if age has never been defined. The value of the result will therefore be undefined and not null. The value undefined means something like The accessed property doesn’t even exist.

Double equal (==) vs Triple equal (===)

Double equal

We use Double equals to compare loose equality. Double equals also perform type coercion.

Understanding the concept

4 == ‘4’ // true

It will show true. This is because of type coercion. JavaScript will try to convert our values into a like type this is called implicit type conversion. In this case, it succeeds. The string value of ‘4’ can easily be converted into the number value of 4. Since 4 equals 4, we get our answer as true.

Triple equal

We use Triple Equals to compare strict equality. This means both the data type and the value we are comparing has to be the same.

Understanding the concept: true

What if we compare number 4, both have the same type and value. As expected, true is returned. Both are numbers, and both share the same value.

4 === 4 // true

--

--