HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Operators

Operators

Math (+ - * / % **), comparison (=== !== < >), and logical (&& || !) operators.

Simple enough for a 5-year-old to understand

Math operators

Example code
1
console.log(7 + 3);
Addition: 10
CodeAddition: 10
2
console.log(7 % 2);
Remainder (mod): 1
CodeRemainder (mod): 1
3
console.log(2 ** 10);
Exponent: 1024
CodeExponent: 1024
4
console.log(7 / 2);
Division: 3.5
CodeDivision: 3.5
Code
💡 Hint: % (modulo) is used to find even/odd: if number % 2 === 0, it is even.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Operators are symbols that perform operations on values: basic arithmetic, comparison, logical combination. In JavaScript, alongside these, there are a few modern operators that significantly ease daily language use — such as optional chaining and nullish coalescing.

The most critical issue with operators is comparison. JavaScript has two equality operators: double equals compares by converting types, while triple equals compares without conversion. In professional code, triple equals is almost always used; double equals is a classic source of unexpected results.

What you will learn

  • Arithmetic and shorthand assignment operators
  • Double and triple equals comparison
  • Logical and, or, not operators
  • making choices in a single line using the conditional operator
  • Optional chaining and nullish coalescing

When to use?

Optional chaining is used when you are not sure if intermediate layers exist in data coming from a server: if any link in the chain is missing, it returns undefined instead of an error.

Nullish coalescing, on the other hand, is for providing a default value and differs from the logical or operator: it only kicks in for null and undefined, treating zero or empty text as valid values.

Common mistakes

Wrong

const count = 0;
const shown = count || 10;
console.log(shown);

The logical or operator falls back to the default value because it considers zero as "false" and prints 10; whereas zero was a valid value. If you want to preserve zero and empty text, the nullish coalescing operator is needed.

Correct

const count = 0;
const shown = count ?? 10;
console.log(shown);

Wrong

const user = {};
console.log(user.profile.name);

Because profile is undefined, trying to read name from inside it throws the error "TypeError: Cannot read properties of undefined (reading 'name')". This is the most common runtime error in JavaScript.

Correct

const user = {};
console.log(user.profile?.name);

Wrong

if (a = 5) {
  console.log("five");
}

A single equals sign performs ASSIGNMENT; assignment inside a condition does not throw an error in JavaScript, the assigned value is considered true, and the block always executes. It is a silent but serious logic error.

Correct

if (a === 5) {
  console.log("five");
}

Frequently asked questions

Should I always use triple equals?

In practice, yes. The only reasonable exception is when you want to check if a value is null or undefined with a single check by comparing with null using double equals; in that case, it catches both. In all other cases, use triple equals.

What is short-circuit evaluation?

In an and chain, when the first falsy is found, or in an or chain, when the first truthy is found, the rest is not executed at all. This is used for safe access: if you check for the existence of an object and then read its property, if the object does not exist, the second part is never evaluated.

Can optional chaining be used everywhere?

It is supported in all modern browsers. However, writing everything with a question mark is not a good idea: you want to know if a field that really should exist is missing. Only use the question mark where it is NORMAL for something to be absent.

Summary

In JavaScript, triple equals is standard for comparison; double equals produces unexpected equalities because it converts types. Optional chaining and nullish coalescing directly solve the two most common errors when working with external data.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code