Home > Web Front-end > JS Tutorial > body text

Js | Truthy & Falsy |

王林
Release: 2024-09-01 21:05:36
Original
514 people have browsed it

Js | Truthy & Falsy |

Truthy and Falsy Values:

in Javascript, truthy and falsy values are used to determine whether a value evaluate to true or false in a boolean
context.this concept is crucial for controlling the flow of your program using conditions like if statement.

Falsy Values: 0," ",null,NaN,false,undefined 
Copy after login
console.log(Boolean(0)); //false
Copy after login
console.log(Boolean(undefined)); //false

Copy after login
console.log(Boolean(' '));  //empty false

Copy after login
console.log(Boolean(null)); //false

Copy after login
console.log(Boolean(NaN)); //false not a number

Copy after login
console.log(Boolean(false)); //false
Copy after login

Truthy Values: any value that is not Falsy :

console.log(Boolean(1)); //true
Copy after login
Copy after login
console.log(Boolean(1833933)); //true
Copy after login
console.log(Boolean(-1)); //true
Copy after login
console.log(Boolean("hello")); //true
Copy after login
console.log(Boolean(1)); //true
Copy after login
Copy after login
console.log(Boolean([])); //true empty array
Copy after login
console.log(Boolean({})); //true empty object
Copy after login
console.log(function (){}); //true
Copy after login
Example:
Copy after login
           t     f
let cash =255  //0 ; 
    //conditions false  statement block not run
if (cash){
  console.log("you can buy burger with drink"); 
}else{
   console.log("you can buy burger"); 
}else{
   console.log("you don't have money"); 
}
Copy after login
let a;
console.log(a); //false
Copy after login
output:
undefined 
Copy after login
let a = 10;
console.log(a); //true
Copy after login
let a = null;
console.log(a); //false
Copy after login

The above is the detailed content of Js | Truthy & Falsy |. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template