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;
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:
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!