Home > Web Front-end > JS Tutorial > What are the Truthy and Falsy Values in JavaScript?

What are the Truthy and Falsy Values in JavaScript?

Patricia Arquette
Release: 2024-12-04 20:07:17
Original
517 people have browsed it

What are the Truthy and Falsy Values in JavaScript?

Understanding JavaScript Truthy and Falsy

The JavaScript language distinguishes between truthy and falsy values. Truthy values evaluate to true when used in a Boolean context, while falsy values evaluate to false.

Consider the following sample data:

var a = 0;
var b = 10 == 5;
var c = 1;
var d = -1;
Copy after login

Understanding Falsy Values

The value var a = 0; is falsy because zero evaluates to false. Similarly, var b = 10 == 5; is falsy because the comparison is false.

Understanding Truthy Values

The value var c = 1; is truthy because non-zero numbers, including negative numbers, evaluate to true. Therefore, var d = -1; is also truthy.

MDN Definition of Truthy/Falsy

According to MDN:

"In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN)."

Comprehensive List of Falsy Values

According to MDN, the complete list of falsy values in JavaScript includes:

  • false
  • null
  • undefined
  • 0
  • NaN
  • '', "", `` (Empty template string)
  • document.all
  • 0n: BigInt
  • -0

The above is the detailed content of What are the Truthy and Falsy Values in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template