Logical Operators in JavaScript Language

Logical Operators

Logical AND ( && ) Operators

Program to use Logical AND ( && ) Operators JavaScript Language

let a = 5;
    let b = 10;
    console.log(true && false);
    console.log(true && true);
    console.log(false && false);
    console.log((a < b) && (a > b));
    

Output

false
    true
    false
    false
    

Steps Description

  • Declare two variables a and b
  • Initialize both variables with value of 5 and 10 respectively a=5, b=10
  • The function of the Logical AND && operator is to check two values, if both the values are true then it returns true otherwise it returns false.
  • The values of a and b variables are displayed with the help of console.log inbuilt function

Logical OR ( || ) Operators

Program to use Logical OR ( || ) Operators JavaScript Language

let c = 10;
    let d = 20;
    console.log(true || false);
    console.log(true || true);
    console.log(false || false);
    console.log((c < d) || (c > d));
    

Output

true
    true
    false
    true
    

Steps Description

  • Declare two variables c and d
  • Initialize both variables with value of 10 and 20 respectively c=10, d=20
  • The function of the logical OR || operator is to check two values, if either value is true, it will return true otherwise it will return false.
  • The values of c and d variables are displayed with the help of console.log inbuilt function

Logical NOT ( ! ) Operators

Program to use Logical NOT ( ! ) Operators JavaScript Language

let e = 50;
    let f = 100;
    console.log(!(e == f));
    console.log(!(e < f));
    console.log(!true > true);
    console.log(!false > false);
    console.log(!true > false);
    

Output

true
    false
    false
    true
    false
    

Steps Description

  • Declare two variables e and f
  • Initialize both variables with value of 50 and 100 respectively e=50, f=100
  • The function of Logical NOT ! operator is to check two values if the value is true then it will be false and if it is false then the value will be true.
  • The values of e and f variables are displayed with the help of console.log inbuilt function