Search This Blog

2023/07/26

AND & OR Logical Operator on Truthy & False values

// If the first operand is falsy, the logical AND operator returns that object.

//and operator
console.log("1:",false && "dog");
console.log("2:",0 && "dog");

//here first operand is truthy
console.log("1.1:","dog" && false);
console.log("2.1:","dog" && "0");

// Output:
// 1: false
// 2: 0
// 1.1: false
// 2.1: 0

// If the first operand is truthy, the logical AND operator returns the second operand.

console.log("3:",true && "dog")
console.log("4:",[] && "dog")

//here also first operand is truthy

console.log("3.1:","dog" && true)
console.log("4.1:","dog" && [])

// Output:
// 3: dog
// 4: dog
// 3.1: true
// 4.1: []

// If the first operand is falsy, the logical OR operator returns the second operand.

//or operator
console.log("5:",false || "dog");
console.log("6:",0 || "dog");

//here first operand is truthy
console.log("5.1:","dog" || false);
console.log("6.1:","dog" || 0);

// Output:
// 5: dog
// 6: dog
//5.1: dog
//6.1: dog

// If the first operand is truthy, the logical OR operator returns that object.

console.log("7:",true || "dog")
console.log("8:",[] || "dog")

//here first operand is truthy
console.log("7.1:", "dog" || true)
console.log("8.1:", "dog" || [])

// Output:
// 7: true
// 8: []
// 7.1: dog
// 8.1: dog

No comments:

Post a Comment