Intern
What logical operators do you know in JavaScript?
sobes.tech AI
Answer from AI
In JavaScript, I know the following logical operators:
- Logical AND (
&&): Returnstrueif both operands are true. Otherwise, returnsfalse. It can be used for short-circuit evaluation: if the first operand is false, the second is not evaluated. - Logical OR (
||): Returnstrueif at least one of the operands is true. Returnsfalseonly if both operands are false. Also supports short-circuit evaluation: if the first operand is true, the second is not evaluated. It is often used for setting default values. - Logical NOT (
!): Inverts the logical value of the operand. Converts the operand to a boolean value before inversion.
Examples of usage:
// Logical AND
let a = true;
let b = false;
console.log(a && b); // false
// Logical OR
console.log(a || b); // true
// Logical NOT
let c = true;
console.log(!c); // false
// Short-circuit evaluation
let result = null;
let defaultValue = "default value";
let finalValue = result || defaultValue; // finalValue will be "default value"
let x = 5;
let y = 10;
if (x > 0 && y > 0) {
console.log("Both are positive");
}