Skip to content
Laviyx Learn

Try

JavaScript course menu

JavaScript Operators: Arithmetic, Assignment, Increment, Logical and More

Learn JavaScript operators: arithmetic, assignment and compound assignment (+=, -=), increment and decrement (prefix vs postfix), comparison, logical AND, OR and NOT, nullish coalescing (??), optional chaining (?.), the ternary operator and precedence.

Beginner8 min readUpdated
On this page

Operators combine values into new values. You have already used arithmetic operators in Numbers and Math; this lesson covers the rest.

Assignment#

= assigns a value. Compound assignment operators update a variable using its current value:

JavaScript
let balance = 1000;
balance += 500;   // balance = balance + 500
balance -= 200;
balance *= 2;
balance /= 4;
console.log(balance);

let message = "Hello";
message += ", world";
console.log(message);
Output
650
Hello, world

Increment and decrement#

++ adds 1 and -- subtracts 1:

JavaScript
let count = 5;
count++;
console.log(count);
count--;
count--;
console.log(count);
Output
6
4

When used inside another expression, position matters: count++ (postfix) gives the old value, then adds; ++count (prefix) adds first and gives the new value:

JavaScript
let a = 5;
console.log(a++);
console.log(a);

let b = 5;
console.log(++b);
Output
5
6
6

To avoid confusion, most code uses ++ only as a statement on its own, or writes count += 1.

Comparison#

OperatorMeaning
===Equal (same type and value)
!==Not equal
>, <, >=, <=Greater, less, and so on
==, !=Loose equality with type conversion (avoid)

They all produce true or false. The next lesson, Comparisons, Truthy and Falsy, covers them in depth.

Logical operators#

OperatorNameTrue when
&&ANDBoth sides are true
||ORAt least one side is true
!NOTFlips true and false
JavaScript
const age = 20;
const hasTicket = true;

console.log(age >= 18 && hasTicket);
console.log(age < 12 || age > 60);
console.log(!hasTicket);
Output
true
false
false

Short-circuiting#

&& and || stop as soon as the result is known, and they return one of their operands, not necessarily true or false:

JavaScript
console.log("Asha" || "Guest");
console.log("" || "Guest");
console.log(0 && "never evaluated");
console.log("logged in" && "show dashboard");
Output
Asha
Guest
0
show dashboard
  • a || b returns a if it is truthy, otherwise b. It was the classic way to provide a default.
  • a && b returns a if it is falsy, otherwise b.

Nullish coalescing: ??#

|| treats all falsy values (0, "", false) as missing, which is often wrong. ?? only falls back when the value is null or undefined:

JavaScript
const savedVolume = 0;
console.log(savedVolume || 50);
console.log(savedVolume ?? 50);

let nickname;
console.log(nickname ?? "No nickname");
Output
50
0
No nickname

Use ?? for default values. ??= assigns only if the variable is currently null or undefined: options.limit ??= 10;.

Optional chaining: ?.#

Reading a property of undefined or null throws an error. ?. stops and returns undefined instead:

JavaScript
const user = { name: "Ravi", address: null };

console.log(user.address?.city);
console.log(user.profile?.photo ?? "default.png");
console.log(user.getAge?.());
Output
undefined
default.png
undefined

This is very useful with data from APIs, where some fields may be missing.

The ternary operator#

condition ? valueIfTrue : valueIfFalse chooses between two values in one expression:

JavaScript
const stock = 0;
const label = stock > 0 ? "In stock" : "Sold out";
console.log(label);
Output
Sold out

typeof and other operators#

  • typeof x gives the type name (Data Types).
  • in checks whether an object has a property: "name" in user.
  • instanceof checks whether an object was created by a class: date instanceof Date.
  • delete obj.prop removes a property.

Precedence#

When an expression has several operators, precedence decides the order. Roughly from first to last: grouping (), member access and calls, ! and typeof, **, * / %, + -, comparisons, ===, &&, || and ??, ? :, and assignment.

JavaScript
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(true || false && false);
Output
14
20
true

&& binds tighter than ||. When in doubt, add parentheses; they cost nothing and make intent clear. (JavaScript does not let you mix ?? with && or || without parentheses, for exactly this reason.)

Practice#

Practice exercise

A cart has const items = 3, const pricePerItem = 120 and an optional const coupon = undefined. Compute the total, apply a discount of coupon?.discount percent, using 0 if there is no coupon, and print Free delivery if the final total is at least 300, otherwise Delivery: ₹40.

Show solution
JavaScript
const items = 3;
const pricePerItem = 120;
const coupon = undefined;

const subtotal = items * pricePerItem;
const discount = coupon?.discount ?? 0;
const total = subtotal - (subtotal * discount) / 100;

console.log(total);
console.log(total >= 300 ? "Free delivery" : "Delivery: ₹40");
Output
360
Free delivery

Key takeaway#

Finished this lesson?