mc0de
JavaScript · Lesson 04 of 17

JavaScript Operators

Make your code do the math! Discover how JavaScript calculates numbers, compares values, updates variables, and combines conditions to create smarter programs.

ArithmeticComparisons Logical OperatorsHands-On Practice
● ● ●   operators.js
const a = 8;
const b = 3;

console.log(a + b);
CONSOLE OUTPUT
> Ready to calculate…
01 / Getting Started

What Are JavaScript Operators?

Imagine building an online store. You need to calculate the total price, check whether a customer qualifies for a discount, and update the quantity in their cart. JavaScript operators make all of these tasks possible.

An operator is a symbol or keyword that performs an operation on one or more values. The values it works with are called operands.

Your first calculation
const price = 20;
const quantity = 3;

const total = price * quantity;

console.log(total);
Console output
60

The multiplication operator * calculates the total. You’ll use operators for everything from simple arithmetic to complex decisions.

02 / Arithmetic Operators

Let JavaScript Do the Math

Arithmetic operators work with numbers. JavaScript supports the familiar mathematical operations, plus a few that may be new to you.

Arithmetic examples
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1
console.log(2 ** 3);  // 8

What Each Operator Does

  • + adds numbers.
  • - subtracts numbers.
  • * multiplies numbers.
  • / divides numbers.
  • % returns the remainder after division.
  • ** raises a number to a power.
Try this: 10 % 3 produces 1 because 3 fits into 10 three times with 1 left over. The remainder operator is especially useful for checking even and odd numbers.
03 / Assignment Operators

Update Values Efficiently

You already know that = assigns a value to a variable. JavaScript also offers shortcuts for updating existing values.

Updating a score
let score = 10;

score += 5;  // Add 5
score -= 2;  // Subtract 2
score *= 2;  // Multiply by 2

console.log(score);
Console output
26

The statement score += 5 is shorthand for score = score + 5.

You can also use /=, %=, and **= for division, remainder, and exponentiation.

04 / Comparison Operators

Compare Values and Get Answers

Comparison operators ask questions about values. Their results are Booleans: either true or false.

Comparing values
console.log(10 > 5);    // true
console.log(10 < 5);    // false
console.log(10 >= 10);  // true
console.log(10 <= 8);   // false
console.log(10 === 10); // true
console.log(10 !== 5);  // true

=== checks strict equality: both values must have the same type and value. !== checks strict inequality.

Why strict equality matters
console.log(5 === "5"); // false
console.log(5 == "5");  // true

The loose equality operator == can convert types before comparing them. Strict equality is generally easier to predict, so it's a good default.

05 / Logical Operators

Combine Multiple Conditions

Sometimes one comparison isn't enough. For example, an online store might offer a discount only when a customer has enough points and their account is active.

Combining conditions
const points = 120;
const isMember = true;

console.log(points >= 100 && isMember);
console.log(points >= 200 || isMember);
console.log(!isMember);
Console output
true true false
  • && means AND. Both conditions must be true for the combined Boolean expression to be true.
  • || means OR. At least one condition must be true.
  • ! means NOT. It reverses a Boolean value.
Good to know: AND and OR can also return non-Boolean values. You'll see why that's useful as you build more advanced programs.
06 / Strings and Operator Precedence

Know the Order of Operations

The plus operator has another job: it can join strings together. This is called concatenation.

Numbers versus strings
console.log(5 + 3);     // 8
console.log("5" + 3);   // "53"
console.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20

When one operand is a string, + may combine text instead of performing addition. JavaScript also follows operator precedence rules, meaning some operations happen before others.

Multiplication normally happens before addition. Parentheses let you make the intended order explicit.

Helpful habit: Use parentheses whenever they make a calculation or condition easier to understand, even when JavaScript could figure out the order without them.
07 / Increment, Decrement & Practical Use

Make Every Calculation Count

Two handy operators are ++ and --. They increase or decrease a numeric variable by one.

Counting items
let cartItems = 2;

cartItems++;
console.log(cartItems); // 3

cartItems--;
console.log(cartItems); // 2

Operators are most useful when combined. Let's calculate a purchase total and determine whether it qualifies for free shipping.

Shopping cart example
const price = 25;
let quantity = 3;

const total = price * quantity;
const freeShipping = total >= 50;

console.log(total);
console.log(freeShipping);
Console output
75 true

One program can use arithmetic to calculate a value and comparisons to make decisions about it. That's a foundation for building interactive websites.

🚀 mC0de Challenge

Build a Mini Shopping Cart Calculator

You're creating a shopping cart for a small online store. Customers should be able to choose a quantity and immediately see their total, including whether they qualify for free shipping.

Your Mission

  1. Create a const for the product price.
  2. Create a let variable for the quantity.
  3. Use multiplication to calculate the subtotal.
  4. Use a comparison operator to check whether the subtotal is at least $50.
  5. Add buttons to increase and decrease the quantity.
  6. Display the updated subtotal and shipping eligibility.
Extra challenge: Apply a 10% discount when the subtotal reaches $100, and show the discounted total. Keep the quantity from dropping below one.
💻 Interactive JavaScript Sandbox

Build Your Shopping Cart!

Change the price, experiment with calculations, and click the quantity buttons in the live result. Can you make the cart qualify for free shipping?

Live Result
Lesson 04 Recap

What You’ve Learned

You're Thinking Like a Programmer!

You can now use operators to perform calculations, update information, and compare values.

  • Perform arithmetic with +, -, *, /, %, and **.
  • Update variables with assignment operators.
  • Compare values using strict equality and other comparisons.
  • Combine conditions with AND, OR, and NOT.
  • Control calculation order with parentheses.
  • Apply operators to an interactive shopping cart.

Next up: Conditional Statements — learn how to make JavaScript choose what happens next!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to calculate numbers, compare values, combine conditions, and update variables using JavaScript operators. You've even built your own interactive shopping cart calculator. Keep up the great work!