47

Can someone please explain JavaScript Truthy and Falsy, using the below sample data. I have read other threads but still confused.

var a = 0;

var a = 10 == 5;

var a = 1; 

var a = -1;

From my understanding, I believe that var a = 1; is the only truthy and the rest are falsy - is this correct?

Tushar
  • 85,780
  • 21
  • 159
  • 179
tonyf
  • 34,479
  • 49
  • 157
  • 246
  • Possible duplicate of [Results of "truthy" and "falsey" is confusing in JavaScript](http://stackoverflow.com/questions/35132997/results-of-truthy-and-falsey-is-confusing-in-javascript) – Dave Newton Feb 29 '16 at 14:51

9 Answers9

81

From my understanding, I believe that var a = 1; is the only truthy and the rest are falsy’s - is this correct?

No.

  1. var a = 0;

    Number zero is falsy. However, note that the string zero "0" is truthy.

  2. var a = 10 == 5;

    This is same as var a = (10 == 5);, so this is falsy.

  3. var a = 1;

    var a = -1;

    Any non-zero number including negative numbers is truthy.

Quoting from 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).

List of falsy values in JavaScript:From MDN

  1. false
  2. null
  3. undefined
  4. 0
  5. NaN
  6. '', "", ``(Empty template string)
  7. document.all
  8. 0n: BigInt
  9. -0
Aashutosh Rathi
  • 763
  • 2
  • 13
  • 28
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • 17
    Why on earth is `document.all` falsy?? – Claudiu Feb 26 '16 at 04:24
  • 7
    @Claudiu _`document.all` has been used for browser detection in the past and the HTML specification defines a willful violation of the ECMAScript standard here to keep compatibility with legacy code (`if (document.all) { // Internet Explorer code here }` or using `document.all` without checking its presence first: `document.all.foo`)._ – Tushar Feb 26 '16 at 04:25
  • 7
    @Tushar: That is wonderfully... brilliant and horrible at the same time. – Claudiu Feb 26 '16 at 04:52
  • Speaking of dupes, I'm pretty sure this one is covered as well :) – Dave Newton Feb 29 '16 at 14:51
  • Hi @DaveNewton, Other conditions except `var a = 10 == 5;` are definitely covered in the _so dupe_. So, IMO should not be closed as dupe. – Tushar Feb 29 '16 at 14:59
  • @Claudiu https://stackoverflow.com/questions/10350142/why-is-document-all-falsy – M. Justin Jun 09 '22 at 18:27
29

There's a simple way to check, which you can use now and forever:

function truthyOrFalsy(a) {
    return a ? "truthy" : "falsy";
}

To wit:

> truthyOrFalsy(0)
"falsy"
> truthyOrFalsy(10 == 5)
"falsy"
> truthyOrFalsy(1)
"truthy"
> truthyOrFalsy(-1)
"truthy"

Also see a list of all falsey values in JavaScript.

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
8

Truthy -> Value that resolve to true in boolean context

Falsy -> Value that resolve to false in boolean context


For better understanding, `falsy` values are given below.
  1. false
  2. 0
  3. empty string
  4. null
  5. undefined
  6. NaN
bajran
  • 1,433
  • 14
  • 23
3

FALSY

  • false
  • 0 (zero)
  • "", '', `` (empty strings)
  • null
  • undefined
  • NaN (not a number)

note : Empty array ([]) is not falsy

TRUTHY

  • Everything that is not FALSY
CharithJ
  • 46,289
  • 20
  • 116
  • 131
2

The below answer might help someone.

As well as a type, each value also has an inherent Boolean value, generally known as either truthy or falsy. Some of the rules are a little bizarre, so understanding the concepts and effect on comparison helps when debugging JavaScript applications.

The following values are always falsy:

  • false
  • 0 (zero)
  • -0 (minus zero)
  • 0n (BigInt zero)
  • '', "", `` (empty string)
  • null
  • undefined
  • NaN

Everything else is truthy. That includes:

  • '0' (a string containing a single zero)
  • 'false' (a string containing the text “false”)
  • [] (an empty array)
  • {} (an empty object)
  • function(){} (an “empty” function)

A single value can therefore be used within conditions. For example:

if (value) { // value is truthy } else { // value is falsy // it could be false, 0, '', null, undefined or NaN }

Manoj
  • 2,000
  • 2
  • 16
  • 21
1

one more check version:

function truthyOrFalsy(a) {
    return (a && "truthy") || "falsy";
}
ktp
  • 126
  • 8
0

In short there are only 6 types of falsy values: You can use this snippet to test them:

function isTruthy(val){
    if(val){
        console.log(val + ' is Truthy');
    }else{
        console.log(val + ' is falsy');
    }
}
    

// all below are truthy
isTruthy (true)
isTruthy ({})
isTruthy ([])
isTruthy (42)
isTruthy ("0")
isTruthy ("false")
isTruthy (new Date())
isTruthy (-42)
isTruthy (12n)
isTruthy (3.14)
isTruthy (-3.14)
isTruthy (Infinity)
isTruthy (-Infinity)

//all below are falsy
isTruthy(0);
isTruthy("");
isTruthy(false);
isTruthy(NaN);
isTruthy(null);
isTruthy(undefined);

Refer this site for details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy

0

Easy way to check Falsy Value and True value

function truthyOrFalsy(val){
  if(val){
    console.log (`${val} is truthy`);
  } else{
    console.log (`${val} is falsy`);
  }   
}

Check all FALSY value:

truthyOrFalsy(false);      //Output: false is falsy
truthyOrFalsy(null);       //Output: null is falsy
truthyOrFalsy(0);          //Output: 0 is falsy
truthyOrFalsy('');         //Output:  is falsy  [blank refers to '']
truthyOrFalsy(NaN);        //Output: NaN is falsy
truthyOrFalsy(undefined);  //Output: undefined is falsy

Please note that undefined is not explicitly used to set as value. Some common scenarios will create undefined:

  • Parameter defined in function but not passed argument in callback function.
  • If nothing returns in function
  • If accessing to an object property/method which is not defined
  • If accessing to an array element which is not defined
function add(num1, num2){   
    console.log(num1, num2);    
}
const result = add(44);
console.log(result);
//Output: 44 undefined
//        undefined

const car = {color:"Blue", price: 200000};
console.log(car.category);
//Output: undefined
arrColors = ["Blue", "Sky", "Purple"];
console.log(arrColors[5]);
//Output: undefined

Check all TRUTHY values

All values are truthy unless they are defined as falsy.

Although ' ', '0', -1, [] could be enlisted to be checked.

truthyOrFalsy(' ');      //Output: is truty     [blank refers to space inside 
                         //                       quote ]
truthyOrFalsy('0');       //Output: 0 is truty 
truthyOrFalsy([]);          //Output: is truty  [blank refers to an empty array]
truthyOrFalsy(-1);         //Output: -1 is truty 
perfectionist1
  • 857
  • 2
  • 11
  • 14
0

Another way to evaluate whether something is truthy or falsy that I like to use is

function truthyOrFalsy(a) {
  return !!a;
}
timSully
  • 137
  • 1
  • 11