42

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
  • 82,599
  • 19
  • 151
  • 169
tonyf
  • 32,737
  • 45
  • 146
  • 230
  • 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

10 Answers10

74

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
  • 667
  • 2
  • 12
  • 26
Tushar
  • 82,599
  • 19
  • 151
  • 169
  • 14
    Why on earth is `document.all` falsy?? – Claudiu Feb 26 '16 at 04:24
  • 6
    @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
25

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
  • 216,039
  • 159
  • 467
  • 667
7

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,341
  • 13
  • 22
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
  • 44,463
  • 20
  • 111
  • 128
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 }

Mano
  • 1,596
  • 2
  • 13
  • 20
1

In JavaScript, && and || don't always produce a boolean value. Both operators always return the value of one of their operand expressions. Using the double negation !! or the Boolean function, "truthy" and "falsy" values can be converted to proper booleans.

true && true =>
true

true && false =>
false

true && 'rahul626'=>
"rahul626"

true && 'i am testing Truthy' && ' upvote it'=>
" upvote it"

Try, tried on console

Well described here

SilverNak
  • 3,123
  • 4
  • 26
  • 36
unsuredev
  • 415
  • 1
  • 6
  • 13
  • Truthy values refer to the objects used in a boolean context and not so much the boolean value that returns true or false. – unsuredev Jan 16 '20 at 08:07
1

one more check version:

function truthyOrFalsy(a) {
    return (a && "truthy") || "falsy";
}
ktp
  • 106
  • 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
  • 637
  • 2
  • 9
  • 14
0

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

function truthyOrFalsy(a) {
  return !!a;
}
timSully
  • 107
  • 1
  • 10