JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Operator Precedence

Operator precedence determines the way in which operators are parsed with respect to each other.

Operators with higher precedence become the operands of operators with lower precedence.

In the following example, multiplication has a higher precedence than addition, which determines the outcome of the equation.

Run code

If instead we would like to run the addition operation first, we should group it in parentheses, which always has the highest precedence.

Run code

When using parentheses, the operations inside the parentheses are computed first.

When many operations have the same precedence (like addition and subtraction), they are computed from left to right:

Run code

Associativity in JavaScript

Expression evaluation is also influenced by the operator associativity.

Associativity means the direction (right to left or left to right) in which entire expression is evaluated.

If two or more operators with the same level of precedence appear in an expression, which will be evaluated first? The operator associativity answers this question.

Consult the following table to resolve any associativity or precedence issue in JavaScript:

Operator Operator Use Operator Associativity Operator Precedence
() Method/function call, grouping Left to right Highest — 1
[] Array access Left to right 1
. Object property access Left to right 1
++ Increment Right to left 2
-- Decrement Right to left 2
- Negation Right to left 2
! Logical NOT Right to left 2
~ Bitwise NOT Right to left 2
delete Removes array value or object property Right to left 2
new Creates an object Right to left 2
typeof Returns data type Right to left 2
void Specifies no value to return Right to left 2
/ Division Left to right 3
* Multiplication Left to right 3
% Modulus Left to right 3
+ Plus Left to right 4
+ String Concatenation Left to right 4
- Subtraction Left to right 4
>> Bitwise right-shift Left to right 5
<< Bitwise left-shift Left to right 5
>, >= Greater than, greater than or equal to Left to right 6
<, <= Less than, less than or equal to Left to right 6
== Equality Left to right 7
!= Inequality Left to right 7
=== Identity operator — equal to (and same data type) Left to right 7
!== Non-identity operator — not equal to (or don't have the same data type) Left to right 7
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional branch Left to right 13
= Assignment Right to left 14
*=, /=, %=, +=,, -=, <<=, >>=, >>>=, &=, ^=, |= Assignment according to the preceding operator Right to left 14
, Multiple evaluation Left to right Lowest: 15