Sobes.tech
Intern

What logical operators do you know in JavaScript?

sobes.tech AI

Answer from AI

In JavaScript, I know the following logical operators:

  1. Logical AND (&&): Returns true if both operands are true. Otherwise, returns false. It can be used for short-circuit evaluation: if the first operand is false, the second is not evaluated.
  2. Logical OR (||): Returns true if at least one of the operands is true. Returns false only 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.
  3. 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");
}