In JavaScript, operator precedence determines the order in which operators are evaluated in an expression.
In JavaScript, operators are symbols that perform operations on variables or values. Operator precedence defines the order in which these operations are carried out. It’s like a set of rules that determine which operation takes priority when multiple operators are used in an expression.
Arithmetic operators perform basic mathematical operations.
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)
let result = 5 + 3 * 2; // Multiplication takes precedence, so result is 11
Comparison operators are used to compare values.
==
(Equality)===
(Strict Equality)!=
(Inequality)!==
(Strict Inequality)
let isEqual = 5 === '5'; // Strict equality checks both value and type, result is false
Logical operators perform logical operations.
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
let isTrue = true && !false; // Logical AND and NOT operations, result is true
=
(Assignment)The assignment operator is straightforward, but it’s crucial for understanding more complex assignments.
let x = 10; // Assigns the value 10 to variable x
These combine arithmetic operations with assignment.
+=
, -=
, *=
, /=
, %=
let y = 5;
y += 3; // Equivalent to y = y + 3; (y is now 8)
?:
(Conditional Operator)
A concise way to write an if-else statement.
let age = 20;
let status = (age >= 18) ? 'Adult' : 'Minor';
Bitwise operators perform operations on binary representations of numbers.
&
, |
, ^
, ~
, <<
, >>
, >>>
Understanding the order in which operators are evaluated is crucial. Here’s a simplified table:
()
.
[]
()
++
--
+
-
!
~
typeof
void
delete
**
*
/
%
+
-
<<
>>
>>>
<
<=
>
>=
in
instanceof
==
===
!=
!==
&
^
|
&&
||
? :
=
+=
-=
*=
/=
%=
,
()
(Highest Precedence)Parentheses are used to explicitly specify the order of evaluation.
let result = (2 + 3) * 2; // Parentheses take precedence, result is 10
.
, []
, ()
(Left-to-Right)Accessing object properties, array elements, and calling functions come next.
let person = { name: 'John', age: 30 };
let nameLength = person.name.length; // Accessing object property and calling length
++
, --
Increment or decrement operations after a variable.
let count = 5;
let newCount = count++; // newCount is 5, count becomes 6
+
, -
, !
, ~
, typeof
, void
, delete
(Right-to-Left)Unary operators perform operations on a single operand.
let num = -5;
let isTrue = !true;
**
(Right-to-Left)Introduced in ES6, it raises the left operand to the power of the right operand.
let square = 2 ** 3; // 2 raised to the power of 3, result is 8
*
, /
, %
Basic arithmetic operations.
let result = 5 * 3 / 2; // Multiplication takes precedence, result is 7.5
+
, -
More arithmetic operations.
let result = 10 + 5 - 3; // Addition takes precedence, result is 12
<<
, >>
, >>>
Shift bits left or right.
let shifted = 8 >> 1; // Right shift by 1, result is 4
<
, <=
, >
, >=
, in
, instanceof
Comparisons and type checks.
let isGreater = 10 > 5; // Greater than comparison, result is true
==
, ===
, !=
, !==
Equality checks and type-strict equality.
let isEqual = 5 === '5'; // Strict equality checks both value and type, result is false
&
Bitwise AND operation.
let bitwiseResult = 5 & 3; // Bitwise AND, result is 1
^
Bitwise XOR operation.
let xorResult = 5 ^ 3; // Bitwise XOR, result is 6
|
Bitwise OR operation.
let orResult = 5 | 3; // Bitwise OR, result is 7
&&
Logical AND operation.
let logicalAndResult = true && false; // Logical AND, result is false
||
Logical OR operation.
let logicalOrResult = true || false; // Logical OR, result is true
,
(Lowest Precedence)Separates expressions, evaluated from left to right.
let result = (2 + 3, 5 * 2); // Uses the result of the last expression, result is 10
Understanding precedence helps in chaining multiple operators for concise expressions.
let result = 2 + 3 * 2 === 8 || (4 % 2) === 0; // Chaining operators for a complex condition
Explicitly using parentheses to control the order of evaluation.
let result = (2 + 3) * 2; // Parentheses override default precedence
Operator precedence is a fundamental aspect of JavaScript that governs how expressions are evaluated. By mastering operator precedence, you gain the ability to Always refer to the operator precedence table when in doubt, and practice with various expressions to solidify your understanding. As you become more comfortable with these concepts, you'll navigate JavaScript expressions with ease. Happy coding and exploring the intricacies of operator precedence in JavaScript! Happy coding !❤️