Truthy and Falsy Values: When All is Not Equal in JavaScript

Comparing two things for equality can often trip up the unwary JavaScript developer, as the language has several quirks we need to be aware of.
In this article, we’ll look at why that is, expl…


Truthy and Falsy: When All is Not Equal in JavaScript

Comparing two things for equality can often trip up the unwary JavaScript developer, as the language has several quirks we need to be aware of.

In this article, we’ll look at why that is, exploring both the double and triple equals operators, as well as the concept of truthy and falsy values in JavaScript. By the time you’ve finished reading, you’ll understand how JavaScript makes its comparisons, as well as how truthy and falsy values can help you write cleaner code.

Truthy and Falsy: When All is Not Equal in JavaScript

Typing in JavaScript

JavaScript variables are loosely/dynamically typed and the language doesn’t care how a value is declared or changed:

let x;
x = 1;   // x is a number
x = '1'; // x is a string
x = [1]; // x is an array

Seemingly different values equate to true when compared with == (loose or abstract equality) because JavaScript (effectively) converts each to a string representation before comparison:

// all true
1 == '1';
1 == [1];
'1' == [1];

Truthy and Falsy: When All is Not Equal in JavaScript

A more obvious false result occurs when comparing with === (strict equality) because the type is considered:

// all false
1 === '1';
1 === [1];
'1' === [1];

Internally, JavaScript sets a value to one of seven primitive data types:

  • Undefined (a variable with no defined value)
  • Null (a single null value)
  • Boolean (a true or false value)
  • Number (this includes Infinity and NaN — not a number!)
  • BigInt (an integer value larger than 2^53 – 1)
  • String (textual data)
  • Symbol (a unique and immutable primitive new to ES6/2015)

Everything else is an Object — including arrays.

Truthy and Falsy: When All is Not Equal in JavaScript

Truthy and Falsy Values in JavaScript

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 falsy:

  • 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)

A single value can therefore be used within conditions. For example:

if (value) {
  // value is truthy
}
else {
  // value is falsy
  // it could be false, 0, '', null, undefined or NaN
}

document.all

You might also see document.all listed as a falsy value. This returns an HTMLAllCollection which contains a list of all of a document’s elements. And while this evaluates to false in a Boolean context, it’s a deprecated feature and MDN advises against its use.

Truthy and Falsy: When All is Not Equal in JavaScript

Continue reading
Truthy and Falsy Values: When All is Not Equal in JavaScript
on SitePoint.


Print Share Comment Cite Upload Translate
APA
Craig Buckler | Sciencx (2024-03-28T14:16:11+00:00) » Truthy and Falsy Values: When All is Not Equal in JavaScript. Retrieved from https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/.
MLA
" » Truthy and Falsy Values: When All is Not Equal in JavaScript." Craig Buckler | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/
HARVARD
Craig Buckler | Sciencx Thursday June 17, 2021 » Truthy and Falsy Values: When All is Not Equal in JavaScript., viewed 2024-03-28T14:16:11+00:00,<https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/>
VANCOUVER
Craig Buckler | Sciencx - » Truthy and Falsy Values: When All is Not Equal in JavaScript. [Internet]. [Accessed 2024-03-28T14:16:11+00:00]. Available from: https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/
CHICAGO
" » Truthy and Falsy Values: When All is Not Equal in JavaScript." Craig Buckler | Sciencx - Accessed 2024-03-28T14:16:11+00:00. https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/
IEEE
" » Truthy and Falsy Values: When All is Not Equal in JavaScript." Craig Buckler | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/. [Accessed: 2024-03-28T14:16:11+00:00]
rf:citation
» Truthy and Falsy Values: When All is Not Equal in JavaScript | Craig Buckler | Sciencx | https://www.scien.cx/2021/06/17/truthy-and-falsy-when-all-is-not-equal-in-javascript/ | 2024-03-28T14:16:11+00:00
https://github.com/addpipe/simple-recorderjs-demo