#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Introduction to operators

Operators are the symbols that tell JavaScript to do something with values: add them, compare them, combine conditions or assign a result to a variable. In total = price * qty, the * multiplies and the = stores the answer. The values operators work on are called operands. Nearly every line of a program uses at least one operator, so knowing the main groups pays off quickly.

The main operator families

GroupOperatorsExample
Arithmetic+ - * / % **10 % 3 gives 1
Assignment= += -= *= /= ??=score += 5
Comparison=== !== < > <= >=age >= 18
Logical&& || !isAdmin || isOwner
Increment / decrement++ --count++
Nullish and optional?? ?.user?.name ?? 'Guest'
Ternarycondition ? a : bn > 0 ? 'positive' : 'not positive'
Typetypeof instanceoftypeof 42 gives "number"

Operators are also grouped by how many operands they take: unary ones such as ! and typeof take one, binary ones such as + take two, and the ternary operator is the only one that takes three.

Trying them out

const price = 250;
let qty = 3;

const total = price * qty;         // 750
const evenQty = qty % 2 === 0;     // false
qty += 2;                          // qty is now 5
const squared = 4 ** 2;            // 16

console.log(5 === '5');            // false: different types
console.log(5 == '5');             // true: loose equality converts types

const name = '';
console.log(name || 'Anonymous');  // 'Anonymous' (empty string is falsy)
console.log(name ?? 'Anonymous');  // '' (only null/undefined fall back)

const user = null;
console.log(user?.email);          // undefined, no error

let a = 1;
console.log(a++);                  // 1 (returns old value, then adds)
console.log(++a);                  // 3 (adds first, then returns)

Operators that behave differently with strings

The + operator does two jobs. With two numbers it adds; if either side is a string, it joins them as text. So 2 + 3 is 5, but '2' + 3 is '23'. The other arithmetic operators convert strings to numbers, so '6' * 2 is 12. This mismatch is the source of many bugs, especially with values read from form inputs, which are always strings. Convert them with Number(input.value) before adding.

Short-circuit logic

&& and || do not always return true or false. They return one of their operands and stop evaluating as soon as the answer is known. a || b returns a if it is truthy, otherwise b. a && b returns a if it is falsy, otherwise b. That is why they are often used for default values and guarded calls, such as callback && callback().

Pitfalls to avoid early

  • Using = in a condition. if (role = 'admin') assigns instead of comparing, and the condition is always true. Use ===.
  • Using ==. Loose equality applies confusing conversions, such as 0 == '' being true. Prefer === and !==.
  • Choosing || for defaults when 0 or an empty string are valid. quantity || 1 turns a real 0 into 1; quantity ?? 1 keeps it.
  • Decimal surprises. 0.1 + 0.2 is not exactly 0.3 because of how numbers are stored; round results for display.

Where to go from here

Learn the families, use strict equality, convert input strings before maths, and pick ?? over || when falsy values are meaningful. When several operators appear in one expression, the order they run in follows precedence rules, which is exactly what the next lesson explains.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course