JS Challenge: 4
Boolean Datatype:
Boolean datatype is one of the primitive datatype of JavaScript like Numbers and Strings. Boolean datatype has two values i.e., true and false. It is represented as follows:
let isHungry = true
We generally use Boolean values in the conditional statements. It's a convention to use Boolean variables with is prefix but we can provide any valid variable names.
Example:
let isHungry = true;
if(isHungry === true){
console.log("Eat food")
}
else{
console.log("Wait for an hour")
}
The above code can be simplified like below using truthy value
let isHungry = true;
if(isHungry){
console.log("Eat food")
}
else{
console.log("Wait for an hour")
}
Note: In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false, 0, -0, 0n, "", null, undefined, and NaN.
Challenge:
- Write a If/else condition using Boolean variables.