Introduction to Operators

Learn what JavaScript operators are, why we need them, and how to use arithmetic, assignment, comparison, logical, and other operator types in your code.

What is a JavaScript Operator?Link to this section

An operator is a special symbol used to perform operations on values and variables. Just like in mathematics, these symbols tell JavaScript to add numbers, compare text, or check conditions.

Here, the plus sign and the equal sign work together to calculate the final price.

  • `+` is the arithmetic operator that performs addition.
  • `=` is the assignment operator that stores the result.
  • `subtotal`, `tax`, and `total` are the variables holding the data.
  • `10`, `2`, and `12` are the number values being processed.

Operators form the backbone of any programming logic, turning static data into dynamic calculations.

Why Do We Need JavaScript Operators?Link to this section

Without operators, code would only be able to store values without ever changing, combining, or testing them.

  • Calculate total shop bills by adding item prices.
  • Check if a student passing mark is greater than or equal to 40.
  • Verify user login credentials by comparing entered passwords.
  • Calculate monthly salaries after deductions and bonuses.
  • Determine if a user is old enough to access a website.
  • Combine first and last names to display a full profile name.

We need operators to adjust prices, calculate taxes, and handle shopping cart totals.

The subtraction operator helps us quickly compute the discounted amount a customer has to pay.

About JavaScript OperatorsLink to this section

Operators take one or more inputs, called operands, and produce a new value. They are grouped by their specific tasks, such as math, comparison, or logic. Understanding how they interact ensures your programs run without unexpected bugs.

Operator CategoryMain PurposeCommon Symbols
ArithmeticPerform math calculations+, -, *, /, %, **, ++, --
AssignmentStore values in variables=, +=, -=, *=, /=
ComparisonCompare two values==, ===, !=, !==, >, <, >=, <=
LogicalCombine multiple conditions&&, ||, !

Operators can modify existing variable values in place using shorthand syntax.

Types of JavaScript OperatorsLink to this section

1. Arithmetic OperatorsLink to this section

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

OperatorNameExample Result
+Addition5 + 2 = 7
-Subtraction5 - 2 = 3
*Multiplication5 * 2 = 10
/Division6 / 2 = 3
%Remainder5 % 2 = 1
**Exponentiation2 ** 3 = 8

note

The remainder operator (%) gives you the leftover value after division, which is great for finding even or odd numbers.

warning

Dividing a number by zero in JavaScript does not crash your program; it returns Infinity.

2. Assignment OperatorsLink to this section

Assignment operators assign values to variables. They can also combine an arithmetic operation with assignment for concise code.

OperatorSame AsExample
x += yx = x + yAdd and assign
x -= yx = x - ySubtract and assign
x *= yx = x * yMultiply and assign
x /= yx = x / yDivide and assign

tip

Use compound assignment operators like `+=` to make your code shorter and easier to read.

3. Comparison OperatorsLink to this section

Comparison operators compare two values and return a boolean result, which is either true or false.

OperatorDescriptionExample
===Strict equal5 === 5 (true)
!==Strict not equal5 !== 3 (true)
>Greater than10 > 5 (true)
<Less than3 < 8 (true)
>=Greater than or equal5 >= 5 (true)
<=Less than or equal4 <= 9 (true)

warning

Always prefer strict equality (===) over loose equality (==) to avoid unexpected type conversion bugs.

4. Logical OperatorsLink to this section

Logical operators combine multiple boolean expressions to make complex decisions in your code.

OperatorNameRule
&&Logical ANDTrue if both sides are true
||Logical ORTrue if at least one side is true
!Logical NOTReverses true to false or false to true

tip

Use the AND operator (&&) when all conditions must be true, and the OR operator (||) when only one condition needs to be true.

5. Increment and Decrement OperatorsLink to this section

Increment and decrement operators increase or decrease a variable's value by exactly 1.

Post-increment: The current value is used first, then increased by one.

Pre-increment: The value is increased by one first, then used.

note

Placing the operator before the variable (pre-increment) changes the value immediately before evaluation, while placing it after (post-increment) changes it after.

Operator PrecedenceLink to this section

Just like in regular math, JavaScript operators follow an order of operations. Multiplication and division happen before addition and subtraction.

tip

Always use parentheses to make your math expressions clear and force the order of calculation you want.

Complete Coding ExampleLink to this section

This complete program calculates a shopping cart total, applies a discount, and checks shipping eligibility.

This script brings together arithmetic, assignment, and comparison operators to process an online purchase.

Important NotesLink to this section

  • Operators are symbols used to perform operations on values and variables.
  • Arithmetic operators include +, -, *, /, %, and **.
  • The assignment operator (=) stores values inside variables.
  • Compound assignment operators like += combine math and assignment.
  • Comparison operators evaluate two values and return true or false.
  • Strict equality (===) checks both value and data type without conversion.
  • Logical AND (&&) requires all conditions to be true.
  • Logical OR (||) requires only one condition to be true.
  • Increment (++) and decrement (--) change variable values by 1.
  • Operator precedence determines the order in which parts of an expression are calculated.
  • Parentheses can override standard operator precedence rules.

SummaryLink to this section

JavaScript operators give your code the power to calculate, compare, and make decisions. By combining arithmetic, comparison, and logical operators, you can build dynamic applications that handle real-world logic like shop bills, user logins, and scoring systems.

Check Your UnderstandingLink to this section

Introduction to JavaScript Operators quiz
Question 1 of 6

What is the output of `let x = 10 % 3; console.log(x);`?

Practice ChallengesLink to this section

1. Tip Calculator

easy

Create two variables for a meal cost of 50 and a tip amount of 10. Use addition to calculate the final total bill and print it.

Expected output

60

Hint: Use the + operator to add meal cost and tip.

2. Discounted Price

easy

A jacket costs 120. Apply a 20 dollar discount using a subtraction assignment operator (-=) and print the new price.

Expected output

100

Hint: Use jacketPrice -= 20;

3. Even or Odd Checker

medium

Use a variable with the number 17 and the remainder operator (%) with 2 to check if the number has a remainder.

Expected output

1

Hint: 17 % 2 will yield 1.

4. Pass or Fail Evaluator

medium

Create a score variable with 75. Write a comparison using >= to check if the score is greater than or equal to 50, and print the boolean result.

Expected output

true

Hint: Compare score >= 50

5. Complex Order Calculation

hard

Calculate the expression (10 + 5) * 2 - 4 using operator precedence rules with parentheses and print the result.

Expected output

26

Hint: Evaluate the parentheses first, then multiply, then subtract.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion