Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  7] [ 7]  / answers: 1 / hits: 58655  / 8 Years ago, fri, february 26, 2016, 12:00:00

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?


More From » javascript

 Answers
5

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


[#63153] Wednesday, February 24, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denism

Total Points: 627
Total Questions: 96
Total Answers: 98

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;