DEV Community

Cover image for Understanding JavaScript Logical Operators: A Deep Dive
Mat Borowiak
Mat Borowiak

Posted on • Edited on

Understanding JavaScript Logical Operators: A Deep Dive

JavaScript is often criticized for its "quirks," and one commonly cited example involves how it evaluates logical expressions. At first glance, some of these results may seem counterintuitive. A classic example is:

console.log(3 < 2 < 1); // true
Enter fullscreen mode Exit fullscreen mode

At first, this might seem like an error or an inconsistency in the language. However, JavaScript follows well-defined rules that explain this behavior logically. This article will break down the key concepts behind such expressions to eliminate confusion and provide a deeper understanding of JavaScript’s operator evaluation.

Key Concepts to Cover:

  1. Logical operators and their behavior
  2. Operator precedence and associativity
  3. Type coercion in JavaScript

By the end of this article, expressions like 3 < 2 < 1 will no longer seem puzzling but will instead make complete sense.


Logical Operators in JavaScript

Logical and comparison operators in JavaScript behave like functions: they take arguments, execute operations, and return results. Consider the following example:

console.log(1 < 2); // true
Enter fullscreen mode Exit fullscreen mode

Here, the < operator compares 1 and 2, returning true because 1 is indeed less than 2.

Even the assignment operator (=) behaves similarly:

console.log(x = 5); // Outputs: 5
Enter fullscreen mode Exit fullscreen mode

The assignment operator not only assigns 5 to x but also returns the assigned value. This behavior is crucial when understanding how JavaScript evaluates expressions.


Operator Precedence and Associativity

Understanding how JavaScript determines the order of operations is key to deciphering complex expressions.

Operator Precedence

Operator precedence dictates which operators execute first when multiple operators appear in an expression. For example:

x = 2 < 3;
Enter fullscreen mode Exit fullscreen mode

Step-by-step evaluation:

  1. The < operator has a higher precedence than =.
  2. 2 < 3 evaluates to true.
  3. x = true assigns true to x.

Operator Associativity

When multiple operators have the same precedence, associativity determines the order of execution. JavaScript’s comparison operators (<, >, <=, >=) are left-to-right associative, meaning they execute from left to right.

Consider:

console.log(1 < 2 < 3); // true
Enter fullscreen mode Exit fullscreen mode

Breaking this down step by step:

  1. 1 < 2 evaluates to true.
  2. true < 3 is then evaluated.

At this point, JavaScript performs type coercion, which is essential to understanding why true < 3 does not result in an error.


Type Coercion in JavaScript

Type coercion is JavaScript’s implicit conversion of values from one type to another. When a non-numeric value is involved in a numerical operation, JavaScript attempts to convert it to a number.

For example:

console.log(Number(true));  // 1
console.log(Number(false)); // 0
Enter fullscreen mode Exit fullscreen mode

Applying this concept to our earlier example:

true < 3  // → 1 < 3 → true
Enter fullscreen mode Exit fullscreen mode

This explains why 1 < 2 < 3 evaluates to true.


Understanding 3 < 2 < 1

Now, let’s analyze the seemingly counterintuitive expression:

console.log(3 < 2 < 1); // true
Enter fullscreen mode Exit fullscreen mode

Breaking it down step by step:

  1. 3 < 2 evaluates to false.
  2. The result, false, is then used in false < 1.
  3. Due to type coercion, false is converted to 0.
  4. 0 < 1 evaluates to true.

Thus, the final result is true, and the behavior aligns with JavaScript’s well-defined rules.


Conclusion

JavaScript’s handling of logical and comparison operators follows a clear set of rules involving precedence, associativity, and type coercion. What may initially seem like a language quirk is actually a logical consequence of these rules.

Understanding these mechanics can help developers write clearer, more predictable JavaScript code while avoiding unexpected results. Next time you see a "JavaScript is weird" meme, you’ll know that the language is simply following its internal logic.

Happy coding!

Top comments (2)

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

Nice article dude ☺ 👌

Collapse
 
roterski profile image
Piotr Roterski

(> 3 2 1)
true

(< 3 2 1)
false

I certainly do not miss js quirks after migrating to clojurescript

repl.replete-web.io/