avatarDr. Derek Austin 🥳

Summary

In JavaScript, truthy values are those that evaluate to true when coerced into a boolean context, with most values, including non-empty objects and arrays, being truthy, except for seven falsy values: 0, 0n, null, undefined, false, NaN, and an empty string.

Abstract

Truthy values in JavaScript are a concept where values are considered true when encountered in a context that requires a boolean. These values encompass almost all primitive types and objects, with the exception of seven specific falsy values. The Mozilla Developer Network defines a truthy value as any value that is not explicitly listed as falsy. The article emphasizes that understanding truthy and falsy values is crucial for writing effective conditional statements in JavaScript, as these values determine the flow of control in the code. While objects and arrays are truthy, even when empty, the concept of truthiness does not strictly apply to them in the same way it does to primitive types, which have explicit falsy representations for empty or zero values. The article also references Stephen Colbert's concept of "truthiness" to humorously highlight the intuitive aspect of truthy values in JavaScript.

Opinions

  • The author suggests that almost everything in JavaScript is a truthy value, emphasizing the importance of knowing the exceptions.
  • The article implies that the concept of truthy values can be useful for writing concise conditional statements, as it allows developers to check for the presence of a value without explicitly comparing it to true.
  • The author points out that while truthy values evaluate to true in a boolean context, they are not loosely equal (==) to the boolean value true, which is an important distinction in JavaScript programming.
  • The author humorously encourages developers to "trust their gut" when expecting a value to evaluate to true, alluding to the sometimes counterintuitive nature of truthy and falsy values in JavaScript.
  • References to external articles and a book by Dr. Derek Austin suggest that further reading on the topic is recommended for a deeper understanding of truthy and falsy values in JavaScript.

What are truthy values in JavaScript?

Truthy values evaluate to true when coerced by JavaScript’s typing engine into a boolean value. Most values are truthy in JavaScript, including the empty array [] and the empty object {}.

Photo by Belinda Fewings on Unsplash

Truthy values in JavaScript

“A truthy value simply means a value that is considered true when evaluated in a boolean context” — Mozilla Developer Network

Any primitive type will evaluate to true in JavaScript, with the exception of the following 7 values, which are called falsy values:

  • the number 0
  • the BigInt 0n
  • the keyword null
  • the keyword undefined
  • the boolean false
  • the number NaN
  • the empty string “” (equivalent to `` or ‘’)

Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {}.

This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.

Specifically, they evaluate to true when given a boolean context, such as an if statement or the question mark e>? operator.

Photo by Ahmed Zayan on Unsplash

Code example of truthy values

The truthy values are called truthy because they evaluate to true, and the falsy values are called falsy because they evaluate to false:

Photo by Hybrid on Unsplash

Conclusion

“Truthiness (noun) — the belief in what you feel to be true rather than what the facts will support” Stephen Colbert

In programming JavaScript, it can be really useful to write a conditional statement to check if a value is set or not.

While it doesn’t work quite right for objects including arrays, primitive types in JavaScript have certain falsy values to mean empty or zero.

Everything else in JavaScript is a truthy value and evaluates to true.

So, trust your gut: your value is going to evaluate to true. 🤨

Photo by chaitanya pillala on Unsplash

Further reading

  • Brandon Morelli explains that while truthy values evaluate to true in a boolean context, they do not loosely equal (==) the boolean value true in an in-depth article over at codeburst.io:
Photo by Nathan Dumlao on Unsplash

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Software Development
Technology
Web Development
Recommended from ReadMedium