Decision Making In JavaScript Using Comparison Operators.

Comparison operators in JavaScript are commonly used to compare or check the relationship between two variables.

Examples are; > Greater than , < Less than, >= Greater than or equals to, <= Less than or equals to , == Equality, != Not equal, === Strict equal, !== Strict not-equal.

All comparison operators return a boolean, they give us one of two values, true or false. Example: 1. alert(5 > 2); //true (correct) 2. alert(5<2); //false (wrong) 3. alert(5 != 2) // true(correct)

Let's take a look at the operator's one after the other with examples.

  1. Greater than operator: Greater than operator evaluates to true if the left operand is greater than the right operand. let a = 3; // greater than operator console.log(a > 1); // true

  2. Greater than or equals to: Greater than or equals to evaluates to true if the left operand is greater than or equal to the right operand. let a = 5; // greater than or equal operator console.log(a >= 5); //true

  3. Less than operator: Less than operator evaluates to true if the left operand is less than the right operand. let a = 3, b = 2;

    // less than operator console.log(a < 2); // false console.log(b < 3); // true

  4. Less than or equal to operator: Less than or equals to operator evaluates to true if the left operand is less than or equal to the right operand. const a = 4;

    // less than or equal operator console.log(a <= 4) // true console.log(a <= 3); // true

  5. Equal operator: Equality operator converts values to numbers, it evaluates to true if the operands are equal.

    1. alert('3' == 3) // true, string '3' becomes a number
    2. alert(true == 1) // true

    Note: Equality check cannot differentiate between 0 and false alert(0 == false) // true;

    1. Not equal operator: Evaluates to true if the operands are not equal. let a = 3, b = 'hi';

    // not equal operator console.log(a != 3); // true console.log(b != 'Hi'); // true

  6. Strict equality operator: Strict equality checks equality === without type conversion. It evaluates to true if the operands are equal and of the same type. In the example below, '3' and 3 are the same numbers, but the data type is different. Strict equality === checks for the data type while comparing. Example:

    1. alert('3' === 3) // false, string isn't converted to a number.
  7. Strict not-equal operator: Strict not-equal operator evaluates to true if the operands are strictly not equal. It's the opposite of strict equal ===. let a = 3, b = 'hello'; // strict not equal operator console.log(a !== 3); // false console.log(a !== '3'); // true console.log(b !== 'Hi'); // true

Decision-making in JavaScript using comparison operators is used to decide whether a statement or block of statements will be executed or not. These examples briefly explain how to use comparison operators in JavaScript.