Skip to main content

Operators and Operator Precedence In JavaScript

Operators and Operator Precedence in JavaScript

Operators and Operator Precedence in JavaScript

In JavaScript, operator precedence determines the order in which operators are evaluated in an expression. Understanding this is crucial for writing predictable code. Using parentheses can clarify the intended order when needed. This article explores key operator types and their precedence, with examples to illustrate their behavior. For a broader introduction, see Introduction to JavaScript.


Arithmetic Operators

Multiplication (*), division (/), and modulo (%) operators have higher precedence than addition (+) and subtraction (-).

console.log(3 + 4 * 5);

Output:

23

Explanation:

  • 4 * 5 is evaluated first due to higher precedence, resulting in 20.
  • 3 + 20 is then evaluated, yielding 23.

For more on arithmetic operations, see JavaScript Math Object.


Comparison Operators

Comparison operators (<, >, <=, >=) have higher precedence than equality operators (==, ===).

console.log(2 + 3 < 4 * 5);

Output:

true

Explanation:

  • 4 * 5 is evaluated first, resulting in 20.
  • 2 + 3 is evaluated next, resulting in 5.
  • 5 < 20 is then evaluated, returning true.

Learn more about comparisons in JavaScript Conditional Statements.


Logical Operators

Logical NOT (!) has the highest precedence, followed by logical AND (&&), then logical OR (||).

console.log(!true && false || true);

Output:

true

Explanation:

  • !true is evaluated first, resulting in false.
  • false && false is evaluated next, resulting in false.
  • false || true is evaluated last, returning true.

For more on logical operators, see JavaScript Conditional Statements.


Assignment Operators

Assignment operators (=, +=, -=) have lower precedence than arithmetic and logical operators.

let x = 2 + 3;
console.log(x);

Output:

5

Explanation:

  • 2 + 3 is evaluated first, resulting in 5.
  • The result is assigned to x using the assignment operator.

Explore variable assignments in JavaScript Variables.


Grouping with Parentheses

Parentheses override default precedence, forcing evaluation of expressions within them first.

let x = (2 + 3) * 4;
console.log(x);

Output:

20

Explanation:

  • (2 + 3) is evaluated first due to parentheses, resulting in 5.
  • 5 * 4 is then evaluated, yielding 20.

Live Demo: Test Operator Precedence


Choosing the Right Operator

  • Arithmetic Operators: Use for calculations, ensuring multiplication/division are evaluated before addition/subtraction.
  • Comparison Operators: Ideal for conditional logic, with precedence over equality checks.
  • Logical Operators: Use for combining conditions, noting that NOT precedes AND, which precedes OR.
  • Assignment Operators: Use for assigning values after other operations are complete.
  • Parentheses: Use to explicitly control evaluation order for clarity and correctness.

Experiment with these operators in our JavaScript Playground!


Real World Applications

Operator precedence is critical in:

  • Calculations: Computing values in financial or scientific applications.
  • Conditional Logic: Building complex conditions for user input validation (see How to Validate an Email Address Using JavaScript).
  • Data Filtering: Combining conditions in data processing.
  • UI Interactions: Calculating positions or styles dynamically.

Tips for Using Operators

  • Use Parentheses: Clarify complex expressions to avoid precedence errors.
  • Understand Precedence: Memorize or reference the precedence table to predict evaluation order.
  • Test Expressions: Use the console or our JavaScript Playground to verify results.

For deeper JavaScript insights, explore A Comprehensive Guide to JavaScript: From Basics to Advanced Topics.


Related Articles

Comments