Page Content

Tutorials

Leveraging Comparison and Logical Operators in JavaScript

Comparison and Logical Operators in JavaScript

JavaScript compares two values using comparison operators. Comparison operators always return true or false Booleans. These operators are essential for program flow and decision-making, utilised in if, while, and for statements.

Usually, two operands are needed for comparison operators. They check to see if two values are related.

Comparison Operators

These operations look for order, inequality, or equality.

  • Equality (==): If the operands have different data types, this operator allows for type conversion (coercion) by determining whether two values are equal. If the values are equivalent after conversion, it returns true; if not, it returns false.
  • Note: Assignment, not equality checking, is the purpose of the single equal sign (=).
  • Strict Equality (===): The operator Strict Equality (===) determines whether two values are strictly equal. Only when the operands are of the same value and data type does it return true. Type conversion is not something it performs. Because its rules are less complicated and less likely to produce unexpected outcomes due to pressure, generally advise favouring ===.
  • Inequality (!=): The converse of == is the inequality operator (!=). After allowing for type conversion, it returns false otherwise and true if the two values are not equal.
  • Strict Inequality (!==): The operator that is the reverse of === is Strict Inequality (!==). If the two values are not exactly equal (either different types or different values), it returns true.

Relational Operators

The order of two values is compared by these operators. They only work for natural ordering data like strings and numbers.

  • Greater than (>): This function returns true if the left operand is greater than the right operand.
  • Less than (<): Function “less than” returns true if left operand is less than right operand.
  • Greater than or equal to (>=): Returns true if left operand > right operand.
  • Less than or equal to (<=): Returns true if left operand is less than or equal to right operand (<=).

Relational operators convert operands that are not strings or numbers when comparing values. Character by character comparisons using Unicode values (lexicographical order) are used in string comparisons. Case sensitivity is important.

// This is a single-line comment
console.log(“Apple” < “Banana”); // true
console.log(“Z” < “a”); // true
console.log(“2” > “12”); // true (string comparison, ‘2’ comes after ‘1’)

Logical Operators

To ascertain the logic between variables or values, logical operators are employed. They can conduct Boolean algebra and are frequently used to combine relational expressions with relational operators. A compound statement that returns a Boolean value can be created by combining two or more statements using logical operators.

  • Logical AND (&&): Use && to determine whether several expressions are true. Only when both operands are true does it return true. It returns false if either operand is false. Short-circuit evaluation is carried out using the && operator, which evaluates the first operand and returns that value right away without evaluating the second operand if it is false.
  • Logical OR (||): You utilise || to obtain true if either of the phrases is true. If at least one operand is true, or if either of the two operands is non-zero (truthy), it returns true. Short-circuit evaluation is another function of the || operator. It evaluates the first operand and, if it is true, returns that value right away without evaluating the second operand.
  • Logical NOT (!): This operator is used to flip the operand’s logical state. It returns the inverse value after converting the operand to a boolean type. ! renders a condition false if it is true, and vice versa.

In order to regulate the course of program execution, conditional expressions such as if and switch frequently include comparison operators and logical operators.

let age = 20;

if (age < 18) { // Using < comparison operator
console.log(“Too young”);
} else { // The alternative block
console.log(“Eligible to vote”);
}

let isAdult = true;
let isStudent = false;

if (isAdult && !isStudent) { // Using && and ! logical operators
console.log(“Adult who is not a student”);
}

Writing programs that can make decisions and react dynamically based on conditions requires an understanding of and proficiency with comparison and logical operators.

Index