avatarDr. Derek Austin 🥳

Summary

The provided web content explains the concept of falsy values in JavaScript, which are values that evaluate to false in boolean contexts, and lists them as 0, 0n, null, undefined, false, NaN, and the empty string "".

Abstract

The article on the website delves into the JavaScript concept of falsy values, which are specific data types or entities that are treated as false when encountered in a context that requires a boolean value. JavaScript's typing engine coerces these falsy values into a boolean false. There are exactly seven falsy values in JavaScript: the number 0 (and its negative counterpart -0), the BigInt 0n, the keyword null, the keyword undefined, the boolean false, the number NaN (Not-a-Number), and the empty string (which can be represented by "" or '' or ``). The article emphasizes that while these values are falsy, they are not necessarily equal to each other when compared using loose equality (==). However, when using strict equality (===), each falsy value is only equal to itself, with the exception of NaN, which is not equal to any value, including itself. The author also touches on the concept of truthy values, which are all values in JavaScript that are not falsy and thus evaluate to true in boolean contexts. The article concludes by encouraging readers to experiment with falsy values in JavaScript conditionals, loops, and logical operators.

Opinions

  • The author suggests that understanding falsy values is crucial for JavaScript developers to prevent potential bugs and crashes in their code.
  • The article implies that JavaScript's approach to handling non-boolean values in boolean contexts is unique compared to other programming languages.
  • It is noted that the concept of falsy values is closely related to type coercion in JavaScript, which can sometimes lead to unexpected results if not properly understood.
  • The author provides further reading resources and encourages the exploration of falsy values in practical coding scenarios, indicating a belief in the importance of hands-on learning and continuous education in programming.
  • The inclusion of links to external articles and a mention of a book on programming success suggests the author values community knowledge sharing and professional development in the field of programming.

What are falsy values in JavaScript?

The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string "". They evaluate to false when coerced by JavaScript’s typing engine into a boolean value, but they are not necessarily equal to each other.

Photo by Matej Drha on Unsplash

Falsy values in JavaScript

“A falsy value is a value that is considered false when encountered in a Boolean context.” — Mozilla Developer Network

In JavaScript, there is a special list of following 7 values, which are called falsy values — they all evaluate to false in conditionals:

Strictly speaking, you have to “coerce” (force) a falsy value to make it false, for example by using Boolean() or the e>? ternary operator.

Of course, the idea of there being falsy values in JavaScript begs the question, what are truthy values in JavaScript?

Truthy values are going to be everything else — anything that is not falsy evaluates to true in a conditional.

Truthy values include the empty object {} and the empty array [] — since they aren’t falsy, they are truthy, by definition.

Photo by Hilthart Pedersen on Unsplash

Code example of falsy values

Here is a complete list of falsy comparisons using the loose equality (==) double equals comparison operator:

Despite all being falsy, they are not all equal with the double equals ==:

  1. The values null and undefined are loosely equal to each other.
  2. NaN is not equal to any other value, not even itself.
  3. The other falsy values (0, 0n, false, and "") are all loosely equal.

A note on strict equality

All the above falsy values would be strictly equal to only themselves using ===, with the exception of NaN, which is the only value not strictly equal to itself in JavaScript.

Here is a complete list of falsy comparisons using the strict equality (===) triple equals comparison operator:

Photo by Emre Gencer on Unsplash

Conclusion

Falsy values are the way that JavaScript deals with not crashing when a conditional is passed something other than a boolean value.

Other programming languages differ, but JavaScript has its certain falsy values, and everything else is going to evaluate to true in a conditional.

The 7 falsy values are: 0, 0n, null, undefined, false, NaN, and "".

The falsy values sort-of loosely equal each other with ==, at least in that 0==0n==false=="", null==undefined, and of course remembering that NaN does not equal anything, not even itself.

Now that you understand falsy statements, try them out inside of conditionals (if statements, question mark e>? conditionals, and loops) in JavaScript.

Photo by Ricardo Gomez Angel on Unsplash

Further Reading

  • The user amrish contributed a great article on falsy values to uxworks:
Photo by James Douglas 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
Recommended from ReadMedium