JavaScript Operators Explained: Types, Usage & Practical Examples

Feat 16733 Png

What if the difference between a working script and a broken one comes down to a single symbol? In JavaScript, operators are everywhere, quietly powering everything from simple calculations to complex logic. Yet, many of us overlook how these tiny characters shape the way our code behaves, leading to confusion and unexpected results.

Key Takeaways

  • Operators are symbols or keywords that help us perform actions like adding, comparing, or assigning values in JavaScript
  • There are several main types of operators in JavaScript, including arithmetic, assignment, comparison, logical, string, bitwise, ternary, and type operators
  • Arithmetic operators help us do basic math, such as addition, subtraction, multiplication, division, and finding the remainder
  • Assignment operators let us give values to variables, for example, using the equal sign to set a variable to a number or a string
  • Comparison operators allow us to check if values are equal, greater than, or less than each other, which is useful for making decisions in our code
  • Logical operators help us combine multiple conditions, so we can check if more than one thing is true or false at the same time
  • The typeof operator helps us find out what kind of data a variable holds, such as a number, string, or boolean

JavaScript Arithmetic Operators

Arithmetic Operators are the operator used to perform arithmetic operations. The arithmetic operation included addition(+), subtraction(-), multiplication(*), division(/), modulus(%), increment(++), and decrement(–).

Operator Name
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
Decrement

Example:

let x = 1;
let y = 2;

let z = x + y;

console.log("Addition: " + z);

Here, we have initialised two variables with some values and then use the + operator to perform addition and print the output in the console.

Output:

Addition: 3

JavaScript Assignment Operators

Assignment Operators are used to assign value to a variable. JavaScript has many assignment operators that can perform arithmetic operators and then assign the given value such as Add and Assign, which first adds the value and then assign it.

Operator Name
= Assign
+= Add and Assign
-= Subtract and Assign
*= Multiply and Assign
/= Divide and Assign
%= Modulus and Assign

Example:

let x = 2;
let y = x; // assign value of x to y

console.log(y); 

y += y; // add y then assign total value to y

console.log(y);

Here we have used the = operator to assign the value of x to y, then used the += operator which first adds the current value of y to itself and then assigns it to itself.

Output:

2
4

JavaScript Comparison Operators

Comparison Operators are used to comparing two values(i.e. operands). There can be multiple ways someone checks for a comparison. JavaScript has many comparison operators that can perform basic to advance level comparisons.

Operator Name
== Equal
!= Not Equal
=== Strict equal
!== Not Strict equal
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

Example:

let x = 2;
let y = 3;

let compare = (x < y);

console.log(compare);

Here we have compared two variables x and y using the < operator, which returns true if the value of x is smaller than y, and returns false if it is not.

Output:

true

JavaScript Logical Operators

Logical Operators are used to perform logical AND, OR and NOT operations.

Operator Name
|| Logical OR
&& Logical AND
! Logical NOT

Example:

let x = true;
let y = false;

let result = x && y;

console.log(result);

Here we have used && operator which returns true only if both side values are equal, if not then returns false.

Output:

false

JavaScript typeof Operator

This operator is used to get the type of a variable.

Example:

let x = 2;

let type = typeof x;

console.log(type);

Output:

number

Want to get the type of Object as well? Don’t worry we have a separate tutorial on it, consider reading it.

JavaScript Ternary Operator

The ternary operator is used for conditional execution. It separates a condition and two expressions with a question mark (?), and the two expressions are also separated with a colon (:), it first checks for the condition, if true then executes the first expression and, if false then executes the second expression.

Example:

let x = 2;
let y = 3;

let ternaryOperator = x < y ? "x is less than y" : "x is greater than y";

console.log(ternaryOperator);

Output:

x is less than y

Common Pitfalls

Using the Assignment Operator Instead of the Comparison Operator

A common mistake is to use the = operator when we mean to compare values, not assign them. For example, writing if (x = 5) will set x to 5 and always return true, which can cause bugs that are hard to find. Instead, we should use == for loose comparison or === for strict comparison. For example, if (x === 5) checks if x is equal to 5 and is also the same type. Always double-check which operator is being used in conditions to avoid unexpected results.

Example:


if (x = 10)
will assign 10 to x and the condition will always be true.

Solution: Use == or === for comparisons.

Correct:
if (x === 10)

Confusing Addition and String Concatenation

The + operator in JavaScript can add numbers or join strings. If we add a number and a string, JavaScript will turn the number into a string and join them. This can lead to unexpected results, especially when working with user input or data from forms.

Example:

let x = “5”; let y = 6; let z = x + y; will result in z being “56”, not 11.

Solution: Make sure both values are numbers before adding. We can use Number() to convert strings to numbers.

Correct: let z = Number(x) + y;

Using Loose Comparison Instead of Strict Comparison

JavaScript has two ways to compare values: == (loose) and === (strict). The loose comparison does not check the type, so 5 == “5” is true. This can cause problems if we expect both value and type to match.

Example:

if (userInput == 0) will be true for userInput = “0” as well as userInput = 0.

Solution: Use === to compare both value and type. This helps

Frequently Asked Questions

What is an operator in JavaScript?

An operator in JavaScript is a symbol or keyword that performs an operation on one or more values or variables, such as addition, subtraction, or comparison.

What are operands in JavaScript?

Operands are the values or variables on which an operator acts, for example, in the expression 2 + 3, both 2 and 3 are operands.

What are the main types of operators in JavaScript?

The main types of operators in JavaScript are arithmetic operators, assignment operators, comparison operators, logical operators, typeof operator, and ternary operator.

How do arithmetic operators work in JavaScript?

Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, division, modulus, increment, and decrement on numbers.

What does the assignment operator do?

The assignment operator sets or updates the value of a variable, for example, x = 5 assigns the value 5 to x.

How are comparison operators used?

Comparison operators compare two values and return a true or false result, such as checking if one value is equal to or greater than another.

What are logical operators in JavaScript?

Logical operators are used to combine or invert boolean values, such as AND, OR, and NOT, to create more complex conditions.

What does the typeof operator do?

The typeof operator checks and returns the data type of a variable or value, like “number” or “string”.

How does the ternary operator work?

The ternary operator is a shortcut for an if-else statement, allowing us to choose between two values based on a condition.

Can operators be used with strings in JavaScript?

Yes, the addition operator can also join, or concatenate, two strings together in JavaScript.

What is the difference between increment and decrement operators?

The increment operator increases a value by one, while the decrement operator decreases a value by one.

Are there operators for working with arrays or objects?

Yes, JavaScript has special operators like the spread operator for arrays and the delete operator for removing properties from objects.

AI Dev Assistant Tips

AI tools such as GitHub Copilot and ChatGPT can make learning and using JavaScript operators much easier. These tools can suggest code, explain how operators work, and even help us write examples or fix mistakes. When we are working with operators like addition, subtraction, or comparison, AI assistants can quickly show us how to use them in real code. This saves time and helps us understand the concepts better.

For example, GitHub Copilot can suggest code snippets as we type, making it faster to see how a specific operator works in a real situation. ChatGPT can answer questions about operator types or explain what a piece of code does. Both tools can also help us write comments or documentation for our code, making our projects easier to read and maintain.

Copy-Paste-Ready AI Prompt

Explain the different types of JavaScript operators with simple code examples for each type. Include a short description for each operator and show how to use them in real code.

  • Start with a clear question or task for the AI, for example, ask for examples of JavaScript operators or explanations of how each operator works.
  • Use short and direct sentences when talking to the AI, this helps the tool understand what we need.
  • Try using the autocomplete suggestions from GitHub Copilot as we type, this can speed up our coding and help us learn new ways to use operators.
  • Ask ChatGPT to explain code or suggest improvements, especially if we are unsure about how an operator works or want to see more examples.
  • Review the AI’s suggestions before using them, this helps us learn and make sure the code fits our project.

Summary 

Operators in JavaScript are used to perform different types of operations on operands. In programming, we perform many operations, JavaScript operator helps exactly to do so. You can perform literally any type of operation in JavavScript using the operator we have discussed above. Hope you find this tutorial useful.

Reference 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

About the Author

Aditya Gupta is a Full Stack Developer with over 3 years of experience building scalable and maintainable web applications. He is passionate about writing clean code and helping other developers level up their skills in the modern JavaScript ecosystem.

Connect on LinkedIn: https://www.linkedin.com/in/dev-aditya-gupta/

My Mission: To demystify complex programming concepts and empower developers to build better software.

Review Your Cart
0
Add Coupon Code
Subtotal