Tutorial Playlist
Operators in JavaScript are the powerhouses behind so many tasks. From the moment we started learning JS development, the simple addition symbol (+) felt incredibly powerful. As we get deeper into JS, the subtle differences between comparison operators (== vs. ===) or the way the assignment operator (=) could be combined with others keep us constantly learning.
Operators become the tools that make coding feel both logical and expressive. I will cover the various operators in JavaScript and explain how we can use them effectively in this tutorial.
Operators are special symbols that tell JavaScript to perform specific actions or computations. The basic operators in JavaScript work by taking values (called operands) and producing a new result.
We can think of them as tools, just like a hammer drives in nails or a calculator crunches numbers, operators do different things to values. We have operators for basic math (+, -, /), comparing if things are equal (==), manipulating logic (&&, ||), and more.
There are also many special operators in JavaScript that carry out highly specialized computational functions. We will learn about all these operators in JavaScript in this guide.
Operators are fundamental to just about any programming language, and JavaScript is no exception. Here's why they're crucial:
Let us discuss the different types of operators in JavaScript. I will explain different operators in JavaScript with the help of examples.
These operators let you perform fundamental math calculations within your JavaScript code. The arithmetic operators follow the usual mathematical order of precedence (often remembered as PEMDAS or BODMAS). JavaScript will attempt to convert non-numeric values to numbers when used with arithmetic operators.
Here are the arithmetic operators:
Example: 10 + 5 // Result: 15
Example: 12 - 3 // Result: 9
Example: 5 * 7 // Result: 35
Example: 25 / 5 // Result: 5
Example: 11 % 3 // Result: 2
Example: 2 ** 3 // Result: 8 (2 to the power of 3)
Example: let x = 5; y = ++x; // x is now 6, y is also 6
Example: let x = 5; y = x--; // x is now 4, y is 5
These operators are used to compare two values, resulting in either true or false. They are essential for making decisions in your code.
Example: `"5" == 5 // Results in true (string "5" is converted to number 5)
Example: `"5" === 5 // Results in false (types don't match)
I also want to mention that comparison operators default to using strict equality (===) in most cases to avoid unexpected type coercion surprises. Comparison results are used in conditional statements (if, else) and loops to control how your program runs.
Logical operators are used to combine and manipulate Boolean (true/false) values or expressions that evaluate true/false. Logical operators are heavily used in if statements to create complex conditions.
Here are the logical operators:
Example: (age >= 18 && hasLicense) // true if the person is 18+ years old and has a driving license
Example: (username === "" || password === "") // true if either username or password is empty
Example: !isLoggedIn // true if the user is not logged in
Note: I want to mention that && and || operate with "short-circuiting," meaning evaluation might stop early if the result is already determined from the first operand. Also, if you enjoy a more formal approach, logical operators can be summarized with truth tables.
The basic assignment operator (=) is the most common operator, it assigns the value on its right side to the variable on its left.
Example: let score = 10;
Assignment with shorthand: These operators combine assignment with other operations, providing a compact way to modify variables:
Addition Assignment (+=): Adds the value on the right to the existing variable and reassigns the new result.
Example: let x = 5; x += 3; // x is now 8
Subtraction Assignment (-=): Subtracts the value on the right from the existing variable and reassigns the result.
Example: let x = 10; x -= 2; // x is now 8
Multiplication Assignment (*=): Multiplies the existing variable with the value on the right and reassigns.
Example: let x = 2; x *= 5; // x is now 10
Division Assignment (/=): Divides the existing variable by the value on the right and reassigns.
Example: let x = 20; x /= 4; // x is now 5
Modulus Assignment (%=): Calculates the remainder after division and reassigns it to the existing variable.
Example: let x = 13; x %= 5; // x is now 3
Exponentiation Assignment (**=): Raises the existing variable to the power of the value on the right and reassigns.
Example: let x = 3; x **= 2; // x is now 9
I would also like to mention these shorthand forms are equivalent to longer expressions (like x = x + 3) but more concise. They are a common feature in many programming languages.
Conditional operators are great for simple conditional assignments where you want to keep your code concise.
Structure: condition ? expressionIfTrue : expressionIfFalse
How it Works:
Example: let age = 25; let message = (age >= 18) ? "Eligible to vote" : "Too young";
Bitwise operators work directly on the binary representation of numbers. Bitwise operators are less common in everyday JavaScript and are often used for low-level programming, network protocols, or optimizations.
Here are the bitwise operators:
Here are some other important operators in JavaScript:
Operator precedence dictates the order in which different operators are executed within an expression. This hierarchy is crucial because it directly affects the calculated outcome.
Why does it matter? Imagine a calculation like this: 10 + 5 * 2
Precedence rules will determine this. It's easiest to see precedence with a table. Operators higher in the table are evaluated before those lower:
Category | Operators | Associativity |
Grouping | () | Left-to-right |
Member Access | . [] ?. | Left-to-right |
Post Increment | ++ -- (postfix) | Left-to-right |
Other | new (object creation) | Right-to-left |
Logical NOT | ! | Right-to-left |
Prefix Increment | ++ -- (prefix) | Right-to-left |
Arithmetic & Other | + - (unary) ~ typeof void delete | Right-to-left |
Exponentiation | ** | Right-to-left |
Arithmetic | * / % | Left-to-right |
Arithmetic | + - | Left-to-right |
Bitwise Shift | << >> >>> | Left-to-right |
Comparison | < <= > >= | Left-to-right |
Equality | == != === !== | Left-to-right |
Bitwise | & | Left-to-right |
Bitwise | ^ | Left-to-right |
Logical | ` | Left-to-right |
Ternary | condition ? expression1 : expression2 | Right-to-left |
Assignment | = += -= *= /= %= **= | Right-to-left |
Other | Left-to-right |
We will now look at the practical applications of computational expression and operators in JavaScript through compilable programs. It is important to try these out yourself as wwell to understand the interaction between variables and operators in JavaScript. Here are some common types of operators in JavaScript with example programs.
Code:
|
Code:
|
Code:
|
Code:
|
If you wish to master JS, you can check out upGrad’s full stack development courses.
Example: (num1 * num2) + (num3 / num4) is clearer at a glance than relying on precedence alone.
Here are some common mistakes we make when working with operators:
Here are two final tips for you that I have gathered from my experience:
Even though I'm comfortable with most operators now, I still sometimes find myself double-checking the nuances of a less common one, or being surprised by the results due to precedence rules.
Operators are a testament to the fact that even in the familiar parts of a language, there's always potential to refine your understanding. Plus, learning how operators might be implemented differently in other languages adds a whole new dimension of appreciation for how JavaScript works!
If you wish to learn software engineering, you can check out upGrad’s software engineering courses.
1. What is the operators in JavaScript?
Operators in JavaScript are symbols that perform operations (like addition, comparison, or logical operations) on values.
2. What is type operator in JavaScript?
The typeof operator returns a string indicating the data type of a value (e.g., "number", "string", "object").
3. When to use == and === in JavaScript?
4. What is the use of or operator in JavaScript?
The OR operator (||) performs logical OR. It returns the first truthy operand, otherwise, the last value if all are falsy.
5. Why do we use operator?
We use operators to manipulate data, write expressions that evaluate to values, and control the flow of our programs.
6. What is the use of in operator?
The in operator checks if a specific property exists within an object or its prototype chain.
mukesh
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .