- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Operator Precedence
In JavaScript, operator precedence ensures the priority of the operators to be executed when a single expression contains multiple operators. So, whatever expressions have higher priority, the compiler executes it first over other operators and then executes the operators with the lower precedence.
Whenever you write any JavaScript expression with only 1 or 2 operators, you can easily understand the output of the expression. But when the expression contains multiple operators, you should know the concept of operator precedence to evaluate the expression correctly.
The best example of operator precedence is that in traditional mathematics, the multiplication operator has higher precedence over the addition or subtraction operator. So, if any mathematical expression contains the multiplication and addition of both operators, you need to perform the multiplication first.
Associativity
The term associativity refers to the direction compiler should follow while evaluating the expression. In many situations, operators have the same precedence. In such cases, ambiguity occurs that which operation the compiler should perform first. So, the compiler takes the help of associativity. It can be from left to right or right to left.
For example, we need to execute the below expression.
let res = 50/5*2;
Considering the above expression as (50/5) * 2 gives 20 as an output.
Evaluating the expression like 50/ (5*2) gives the 5 as a resultant value.
To resolve the above ambiguity, the compiler uses the associativity rule. The associativity for the division and multiplication operator is from left to right. So, it evaluates the expression as (50 / 5) * 2.
The assignment operator has right-to-left associativity. Consider the below assignment expression.
P = q = 90;
In the above expression, 90 is assigned to the q, and the value of the q variable is assigned to the p.
In short, the JavaScript compiler evaluates the expression based on the operator precedence, and when multiple operators have the same precedence, it uses the associativity rule.
Operator Precedence Table
The below table contains the operator, its description, associativity direction, and a short example.
Operator Precedence | Operator | Description | Associativity | Example |
---|---|---|---|---|
1 | () | Grouping | L -> R | (expression) |
2 | . | Member of object | L -> R | Object_name.property |
2 | () | Function call | L -> R | Demo() |
2 | new | To create objects | R -> L | New test() |
2 | [] | Member of object | L -> R | Object["property"] |
3 | -- | Postfix decrement | - | p--; |
3 | ++ | Postfix increment | - | p++ |
4 | -- | Prefix decrement | R -> L | --p; |
4 | ++ | Prefix increment | R -> L | ++p; |
4 | typeof | To get the variable type | R -> L | typeof a; |
4 | ! | Logical not | R -> L | !a; |
4 | ~ | Bitwise not | R -> L | ~p |
4 | - | Unary minus | R -> L | -p |
4 | + | Unary plus | R -> L | +p |
4 | delete | To delete object property | R -> L | Delete arr[0] |
4 | void | Evaluates void | R -> L | Void(1) |
5 | ** | Exponentiation operator | R -> L | p ** q |
6 | * | Multiplication | L -> R | p * q |
6 | / | Division | L -> R | p / q |
6 | % | modulo | L -> R | p % q |
7 | + | Addition or plus operator | L -> R | p + q |
7 | - | Subtraction operator | L -> R | p - q |
8 | << | Left shift | L -> R | p << 2 |
8 | >> | Signed right shift | L -> R | p >> 2 |
8 | >>> | Unsigned right shift | L -> R | p >>> 2 |
9 | in | Property in object | L -> R | x in y |
9 | instanceof | Instance of object | L -> R | p instanceof Object |
9 | < | Less than | L -> R | p < q |
9 | <= | Less than or equal to | L -> R | p <= q |
9 | > | Greater than | L -> R | p > q |
9 | >= | Greater than or equal to | L -> R | p >= q |
10 | == | Equality | L -> R | p == q |
10 | != | Inequality | L -> R | p != q |
10 | === | Strict equality | L -> R | p === q |
10 | !== | Strict inequality | L -> R | p !== q |
11 | & | Bitwise AND | L -> R | p & q |
12 | ^ | Bitwise XOR | L -> R | p ^ q |
13 | | | Bitwise OR | L -> R | p | q |
14 | && | Logical AND | L -> R | p && q |
15 | || | Logical OR | L -> R | p || q |
16 | ?? | Nullish Coalescing | R -> L | p ?? q |
17 | = | Assignment | R -> L | p = q |
17 | : | Colon assignment | R -> L | p : q |
17 | += | Addition assignment | R -> L | p += q |
17 | -= | Subtraction assignment | R -> L | p -= q |
17 | *= | Multiplication assignment | R -> L | p *= q |
17 | /= | Division assignment | R -> L | p /= q |
17 | %= | Modulo assignment | R -> L | p %= q |
17 | **= | Exponentiation assignment | R -> L | p **= q |
17 | <<= | Left shift assignement | R -> L | p <<= q |
17 | >>= | Right shift assignment | R -> L | p >>= q |
17 | >>>= | Unsigned right shift assignment | R -> L | p >>>= q |
17 | &= | Bitwise AND assignment | R -> L | p &= q |
17 | ^= | Bitwise XOR assignment | R -> L | p ^= q |
17 | |= | Bitwise OR assignment | R -> L | p |= q |
17 | &&= | Logical AND assignment | R -> L | p &&= q |
17 | ||= | Logical OR assignement | R -> L | p ||= q |
17 | => | Arrow operator | - | (a, b )=> { // function code} |
17 | … | Spread operator | - | [… arr] |
18 | yield | Pause / Resume | R -> L | yield p; |
19 | , | Comma operator | L -> R | (10, 20, 30) |
Examples
Let's understand the operator precedence via simple examples.
Example
In the example below, the first expression contains the division, modulo, and multiplication operators with the same precedence. So, the compiler will use the associativity rule, which is left to right for multiplication, division, and modulo operator.
So, it divides the 30 by 15, takes modulo of (30/15) with 3, and multiples the ((30/15)%3) with 2.
In the second expression, the exponentiation operator has right-to-left associativity. So, it evaluates the expression same as (2 *8 (3 ** 2)).
<html> <body> <div id = "output"></div> <script> const first = 30 / 15 % 3 * 2; const second = 2 ** 3 ** 2; document.getElementById("output").innerHTML = "The value of first expression is : " + first + "<br>" + "The value of second expression is : " + second; </script> </body> </html>
Output
It will produce the following result −
The value of first expression is : 4 The value of second expression is : 512
Example
This code demonstrates that you can use the grouping operator () to change the operator precedence. In the below code, we have taken the same expressions which we have taken in the above code, but we change the operator precedence.
In the first expression, first, we take modulo and multiply the resultant value with 2. So, we get 0 and divide 30 by 0, returning infinity.
In the second expression, the first expression evaluates the (2 ** 3) and (8 ** 2), which is equal to 64.
<html> <body> <div id = "output"></div> <script> const first = 30 / ((15 % 3) * 2); const second = (2 ** 3) ** 2; document.getElementById("output").innerHTML = "The value of first expression is : " + first + "<br>" + "The value of second expression is : " + second; </script> </body> </html>
Output
The value of first expression is : Infinity The value of second expression is : 64
The grouping operator can change operator precedence for any operator as it has the highest operator precedence.