Home > Web Front-end > JS Tutorial > What are All the Falsey Values in JavaScript?

What are All the Falsey Values in JavaScript?

Susan Sarandon
Release: 2024-12-18 10:43:20
Original
320 people have browsed it

What are All the Falsey Values in JavaScript?

All Falsey Values in JavaScript

When evaluating expressions in JavaScript, certain values are considered "falsey." These values result in a Boolean evaluation of false when used in contexts such as if(value), value ? and !value.

Falsey Values in JavaScript:

  • false: The literal Boolean value for false.
  • 0 and -0: Numbers representing zero, including hexadecimal 0x0.
  • 0n and 0x0n: Zero of the BigInt type.
  • '', "", and ``: Strings with length 0.
  • null: A special value used to represent an empty or unknown value.
  • undefined: A special value indicating an uninitialized or absent value.
  • NaN: Not-a-Number, representing a numerical value that is not valid.

Special Case: document.all (HTML Browsers Only)

  • This object, which existed in IE before IE11, is now standardized and is technically falsey. However, it was used in early versions of IE to detect the existence of the browser.

ToBoolean Function and Truthiness

The "falsey" nature of values is determined by JavaScript's internal ToBoolean function. This function converts a value to a Boolean value according to the following rules:

  • Undefined: false
  • Null: false
  • Boolean: Value itself
  • Number: false if 0, -0, or NaN; true otherwise
  • String: false if empty; true otherwise
  • BigInt: false if 0n; true otherwise
  • Symbol: true
  • Object: true

Understanding falsey values is crucial for writing robust and efficient code in JavaScript, as these values can affect the flow of execution and the evaluation of logical expressions.

The above is the detailed content of What are All the Falsey 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