#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

Deep dive into operators

Operators are the symbols and keywords that take one or more values, called operands, and produce a new value. JavaScript has many families of them beyond basic maths, and knowing how each one behaves, especially with mixed types, helps you read other people's code and avoid subtle bugs.

Operators grouped by what they do

FamilyExamplesProduces
Arithmetic+ - * / % **A number (or a string for + with text)
Assignment= += -= ??= ||=Stores a value in a variable
Comparison=== !== < >=A boolean
Logical&& || !One of the operands, or a boolean for !
Nullish and optional?? ?.A fallback value or undefined
Type and otherstypeof instanceof in ... ?:Varies

Operators also differ by how many operands they take. ! and typeof are unary (one operand), most are binary (two), and the conditional ? : is the only ternary operator.

Logical operators return values, not just true or false

This surprises many learners. a || b returns a if it is truthy, otherwise b. a && b returns a if it is falsy, otherwise b. They also short-circuit: the right side is not evaluated at all when the left side already decides the result. That is why user && user.name is safe even when user is null.

The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else, including "0", empty arrays and empty objects, is truthy.

?? versus ||, and optional chaining

Using || for defaults has a hidden flaw: it replaces every falsy value. If a user genuinely sets a quantity of 0, qty || 1 turns it into 1. The nullish coalescing operator ?? only falls back when the left side is null or undefined, so qty ?? 1 keeps the 0.

Optional chaining ?. stops and returns undefined if the value before it is null or undefined, instead of throwing a TypeError.

const settings = { volume: 0, theme: null, profile: { city: 'Pune' } };

console.log(settings.volume || 50);       // 50  (0 was treated as "missing")
console.log(settings.volume ?? 50);       // 0   (0 is kept)
console.log(settings.theme ?? 'light');   // 'light'

console.log(settings.profile?.city);      // 'Pune'
console.log(settings.address?.city);      // undefined, no error

let retries;
retries ??= 3;                            // assigns only if null/undefined
console.log(typeof retries);              // 'number'
console.log('volume' in settings);        // true

Type-checking operators and their quirks

  • typeof returns a string such as "number", "string", "object" or "function". Watch out: typeof null is "object", a long-standing quirk, and arrays also report "object". Use Array.isArray() for arrays.
  • instanceof checks whether an object was created from a particular class or constructor, such as err instanceof TypeError.
  • in checks whether a property name exists on an object, even if its value is undefined.

A mixing mistake the language blocks

You cannot combine ?? directly with || or && without parentheses. Writing a || b ?? c is a syntax error on purpose, because the intended order would be unclear. Add brackets: (a || b) ?? c. More generally, when several operators share a line, brackets make the meaning obvious to the next reader, even if the precedence rules from the previous lesson would give the same result.

Main points

  • && and || return one of their operands and skip the right side when they can.
  • Use ?? for defaults when 0 or an empty string are valid values.
  • Use ?. to read nested data safely, and remember that typeof null is "object".

Ready to master real-world web development?

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

Explore Course