Home > Web Front-end > JS Tutorial > What JavaScript Values Are Truthy and Which Are Falsy?

What JavaScript Values Are Truthy and Which Are Falsy?

Linda Hamilton
Release: 2024-11-29 04:54:16
Original
808 people have browsed it

What JavaScript Values Are Truthy and Which Are Falsy?

Understanding JavaScript Truthy and Falsy

In JavaScript, certain values evaluate to true or false in Boolean contexts. This concept is known as truthiness and falsiness. To understand this, let's examine the code provided:

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

Answers to Your Questions:

Contrary to your belief, only two of the statements result in truthy values:

  • var a = 1;: Non-zero numbers, including negative values, are truthy.
  • var a = -1;: Negative values are also truthy.

Falsy Values:

The remaining statements evaluate to falsy values:

  • var a = 0;: Zero is falsy. (However, note that "0" as a string is truthy)
  • var a = 10 == 5;: This is equivalent to (10 == 5), which is a comparison that evaluates to false.

MDN Definition of Truthiness:

As per the Mozilla Developer Network (MDN), a truthy value is one that translates to true when evaluated as a Boolean. All values are considered truthy except for the following:

  • false
  • null
  • undefined
  • 0
  • NaN
  • "" (empty strings)

The above is the detailed content of What JavaScript Values Are Truthy and Which Are Falsy?. 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