English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript basic tutorial

JavaScript object

JavaScript function

JS HTML DOM

JS browser BOM

AJAX basic tutorial

JavaScript reference manual

JavaScript Operator Precedence

The precedence of operators determines how they parse each other.

The operator with higher precedence will become the operand of the operator with lower precedence.

In the following example, the precedence of multiplication is higher than that of addition, which determines the result of the operation.

// First, add3Multiply5, then add10
var x = 10 + 3 * 5;
Test to see‹/›

On the contrary, if we want to run the addition operation first, we should group it in parentheses, which always has the highest precedence.

// First, add10And3Add, then multiply5
var x = (10 + 3) * 5;
Test to see‹/›

When using parentheses, the operations inside the parentheses should be calculated first.

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

var x = 10 + 3 - 5;
Test to see‹/›

JavaScript's associativity

The evaluation of expressions is also affected by the associativity of operators.

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

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

Please refer to the table below to resolve any related or precedence issues in JavaScript

OperatorUsage DescriptionOperator AssociationOperator Precedence
()Method/Function Call, GroupingLeft to RightMaximum- 1
[]Array AccessLeft to Right1
.Object Property AccessLeft to Right1
++IncrementRight to Left2
--DecrementRight to Left2
-Subtraction
Right to Left2
!Logical NOTRight to Left2
~Bitwise NOTRight to Left2
deleteDelete array values or object propertiesRight to Left2
newCreate an objectRight to Left2
typeofReturn Data TypeRight to Left2
voidDo not specify the value to be returnedRight to Left2
/DivisionLeft to Right3
*MultiplicationLeft to Right3
%ModulusLeft to Right3
+AdditionLeft to Right4
+String ConcatenationLeft to Right4
-SubtractionLeft to Right4
>>Bitwise Right ShiftLeft to Right5
<<Bitwise Left ShiftLeft to Right5
>, >=Greater Than, Greater Than or EqualLeft to Right6
<, <=Less Than, Less Than or EqualLeft to Right6
==EqualLeft to Right7
!=Not EqualLeft to Right7
===Consistent/Strictly Equal (Same data types)Left to Right7
!==Not Consistent/Strictly Not Equal (Different data types)Left to Right7
&Bitwise ANDLeft to Right8
^Bitwise XORLeft to Right9
|Bitwise ORLeft to Right10
&&Logical ANDLeft to Right11
||Logical ORLeft to Right12
?:Conditional BranchLeft to Right13
=AssignmentRight to Left14
*=, /=, %=, +=,, -=, <<=, >>=, >>>=, &=, ^=, |=Allocation according to the preceding operatorRight to Left14
,Multiple EvaluationLeft to RightMinimum:15