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

What are Truthy and Falsy Values in JavaScript?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-02 10:30:11493browse

What are Truthy and Falsy Values in JavaScript?

Understanding JavaScript Truthy and Falsy Values

In JavaScript, certain values are evaluated as either truthy or falsy in Boolean contexts. These evaluations are crucial for conditional statements, logical operations, and other aspects of the language.

Regarding the sample data you provided:

  1. var a = 0: Falsy. Zero evaluates to false in a Boolean context.
  2. var a = 10 == 5: Falsy. The comparison operator == returns true if the values are equal. Since 10 is not equal to 5, it evaluates to false.
  3. var a = 1: Truthy. Non-zero numbers are truthy.
  4. var a = -1: Truthy. Negative numbers are also truthy.

MDN Definition of Truthy and Falsy:

According to MDN (Mozilla Developer Network), a truthy value evaluates to true when used in a Boolean context, while all other values are considered falsy.

List of Falsy Values in JavaScript:

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. '' (Empty string)
  7. Empty string
  8. document.all
  9. 0n (BigInt)
  10. -0

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

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