These are the notes for JavaScript episode 3. You will learn the arithmetic, assignment, comparison and logical operators, operator precedence, why "18" + 1 is "181" (type coercion), Number() and NaN, why you should always use ===, truthy and falsy values, short-circuiting, and the ?? operator for safe defaults. Every example runs as-is in the SyntaxPilot compiler (language: JavaScript, Node.js).
Arithmetic operators: splitting a dhaba bill
Four friends finish dinner. The bill is ₹450.
| At the dhaba | In JavaScript | Result |
|---|---|---|
| Add up the bill | 250 + 120 + 80 |
450 |
| Split ₹450 among 4 | 450 / 4 |
112.5 (decimals kept) |
| Only whole notes | Math.floor(450 / 4) |
112 (rounds down) |
| What's left over? | 450 % 4 |
2 (the remainder) |
const bill = 450;
const friends = 4;
console.log(bill / friends);
console.log(Math.floor(bill / friends));
console.log(bill % friends);
console.log(2 ** 10);
112.5
112
2
1024
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
add (or glue text) | 5 + 3 |
8 |
- |
subtract | 5 - 3 |
2 |
* |
multiply | 5 * 3 |
15 |
/ |
divide (exact) | 7 / 2 |
3.5 |
% |
remainder | 7 % 2 |
1 |
** |
power | 2 ** 10 |
1024 |
An operator is a symbol that does something to values; the values are its operands. Unlike Java or C, / never drops decimals in JavaScript, because all numbers are just number. Dividing by zero doesn't crash: 10 / 0 is Infinity, and 0 / 0 is NaN.
Precedence: which operator goes first
Aarav scored 80 and 90. The average should be 85:
console.log(80 + 90 / 2);
125
/ runs before + (BODMAS), so it computed 80 + 45. This order is operator precedence. Brackets always go first:
console.log((80 + 90) / 2);
85
Order, highest first: ( ) → ** → * / % → + - → comparisons → ===/!== → && → ||/?? → =. When in doubt, add brackets.
++ and compound assignment: the mela counter
let visitors = 0;
visitors++;
visitors++;
visitors += 10;
console.log("Visitors: " + visitors);
let bill = 100;
bill -= 20;
bill *= 2;
console.log("Bill: " + bill);
Visitors: 12
Bill: 160
| Shortcut | Same as |
|---|---|
x++ |
x = x + 1 |
x-- |
x = x - 1 |
x += 10 |
x = x + 10 |
x -= 20 |
x = x - 20 |
x *= 2 |
x = x * 2 |
x /= 2 |
x = x / 2 |
++ is an assignment, so it fails on a const:
const count = 0;
count++;
TypeError: Assignment to constant variable.
Comparisons: the voter ID check
const age = 20;
console.log(age >= 18);
console.log(age < 18);
console.log(age === 20);
true
false
true
| Operator | Question |
|---|---|
> < |
greater / less than? |
>= <= |
greater / less than or equal? |
=== |
exactly equal (same value and type)? |
!== |
not exactly equal? |
Every comparison returns a boolean. One = assigns; three === ask a question.
The "181" trap: type coercion
Anything typed into a form or read from the keyboard arrives as a string:
const age = "18"; // from a form: text!
console.log(age >= 18);
console.log(age + 1);
console.log(age - 1);
true
181
17
Type coercion = JavaScript silently converting a type for you.
+has two jobs, add and glue. If either side is a string, it glues:"18" + 1→"181".-,*,/only do maths, so they convert the string to a number:"18" - 1→17.
The fix: explicit conversion. Convert input yourself with Number():
const age = Number("18");
console.log(age + 1);
console.log(Number("abc"));
console.log(typeof NaN);
19
NaN
number
NaN: Not a Number
NaN means "this maths made no sense". Its type is number (like a calculator showing Error: still the calculator's answer, just a broken one). NaN is the only value not equal to itself:
const marks = Number("ninety");
console.log(marks === NaN);
console.log(Number.isNaN(marks));
false
true
Always check with Number.isNaN().
== vs ===: two watchmen
| At the office gate | Operator | Rule |
|---|---|---|
| Relaxed watchman: "looks like Ravi, come in" | == |
converts types, then compares (loose) |
| Strict watchman: face and ID card | === |
same value and same type (strict) |
console.log(5 == "5");
console.log(0 == "");
console.log(0 == false);
console.log(null == undefined);
true
true
true
true
console.log(5 === "5");
console.log(0 === "");
console.log(0 === false);
console.log(null === undefined);
false
false
false
false
Rule: always use === and !==. Convert types yourself first.
Comparing text compares like a dictionary
const a = "10"; // typed in a form
const b = "9";
console.log(a > b);
console.log(Number(a) > Number(b));
false
true
Two strings compare letter by letter: "1" comes before "9". Convert to numbers before comparing.
Logical operators: rules at the cinema
| At the cinema | Operator | Meaning |
|---|---|---|
| Adult film: 18+ AND a ticket | && |
true only if both are true |
| Discount: student OR senior | \|\| |
true if at least one is true |
| NOT got a ticket | ! |
flips true/false |
const age = 20;
const hasTicket = true;
console.log(age >= 18 && hasTicket);
const isStudent = false;
const isSenior = true;
console.log(isStudent || isSenior);
console.log(!hasTicket);
true
true
false
Short-circuiting
const bill = 450;
const friends = 0;
console.log(bill / friends);
Infinity
&& stops at the first false value (and || at the first true one), so put the safety check first:
const bill = 450;
const friends = 0;
console.log(friends !== 0 && bill / friends > 100);
false
The division never ran.
Truthy and falsy
Every value acts like true or false in a logical context. Only six values are falsy:
false, 0, "" (empty string), null, undefined, NaN
Everything else is truthy, including "0", " " and -5.
console.log(Boolean(0), Boolean(""), Boolean(null));
console.log(Boolean(undefined), Boolean(NaN));
console.log(Boolean("0"), Boolean(" "), Boolean(-5));
false false false
false false
true true true
Safe defaults: || vs ??
A shop's default discount is 10%, but today the owner sets 0% on purpose:
let discount = 0; // 0% on purpose
console.log(discount || 10);
console.log(discount ?? 10);
let coupon; // never set
console.log(coupon ?? "NONE");
10
0
NONE
a || breturnsaif it's truthy, elseb.0is falsy, so a real0gets replaced.a ?? b(nullish coalescing) returnsbonly whenaisnullorundefined(nullish).
Use ?? for defaults.
Common mistakes (and what really happens)
| You wrote | What happens | Why | Fix |
|---|---|---|---|
const age = "18"; age + 1 |
(no error) "181" |
+ glues when one side is text |
Number(age) + 1 |
80 + 90 / 2 |
(no error) 125 |
/ before + |
(80 + 90) / 2 |
0 == "" |
(no error) true |
== converts types |
0 === "" → false |
x === NaN |
(no error) always false |
NaN ≠ NaN | Number.isNaN(x) |
"10" > "9" |
(no error) false |
dictionary order | Number("10") > Number("9") |
bill / 0 |
(no error) Infinity |
no crash on ÷0 | check friends !== 0 && first |
discount \|\| 10 with 0 |
(no error) 10 |
0 is falsy | discount ?? 10 |
const count = 0; count++; |
TypeError: Assignment to constant variable. |
++ reassigns |
let count |
10++; |
SyntaxError: Invalid left-hand side expression in postfix operation |
can't change a literal | use a variable |
Mini project: a bill splitter
// Mini project: a bill splitter (input arrives as text)
const billText = "450";
const friendsText = "4";
const bill = Number(billText);
const friends = Number(friendsText);
console.log("Each pays (exact): " + bill / friends);
console.log("Each pays (notes): " + Math.floor(bill / friends));
console.log("Left over: " + bill % friends);
console.log("Valid input? " + (friends > 0 && !Number.isNaN(bill)));
Each pays (exact): 112.5
Each pays (notes): 112
Left over: 2
Valid input? true
Practice
- Split ₹1,000 among 3 friends: exact share, whole-note share and what's left over.
- Predict, then run:
"2" * "3","2" + "3",true + 1. - Write three comparisons where
==and===disagree. - Default a missing name to
"Guest"without replacing an empty string"".
Solutions and hints
1000 / 3→333.3333333333333,Math.floor(1000 / 3)→333,1000 % 3→1.6(both converted to numbers),"23"(glued),2(truebecomes1).5 == "5"vs5 === "5";0 == falsevs0 === false;null == undefinedvsnull === undefined.let name; console.log(name ?? "Guest");printsGuest; withname = ""it prints an empty line, because??only replacesnull/undefined.
Quick quiz
What does this print?
console.log("5" + 3 - 2);
- A.
6 - B.
51 - C.
"532" - D.
NaN
Answer
B. Left to right: "5" + 3 glues into "53"; then - only does maths, so "53" becomes 53, and 53 - 2 = 51.
Interview questions
- What's the difference between
==and===? Which should you use? - What is type coercion? Give an example with
+and with-. - List the falsy values in JavaScript.
- Why is
NaN === NaNfalse? How do you test for NaN? - What's the difference between
||and??? - What is short-circuit evaluation? Give a practical use.
- What does
10 / 0return in JavaScript?
Glossary
| Term | Meaning | Example |
|---|---|---|
| Operator | a symbol that does something to values | +, ===, && |
| Operand | a value an operator works on | 5 and 3 in 5 + 3 |
Remainder % |
what's left after dividing | 450 % 4 → 2 |
Power ** |
raise to a power | 2 ** 10 → 1024 |
Math.floor |
round down to a whole number | Math.floor(112.5) → 112 |
| Precedence | which operator runs first | * before + |
| Compound assignment | operate and assign in one step | x += 10 |
Increment ++ |
add one | visitors++ |
| Comparison operator | asks a question, returns a boolean | age >= 18 |
| Type coercion | automatic type conversion | "18" + 1 → "181" |
| Explicit conversion | converting on purpose | Number("18") |
NaN |
"Not a Number": result of invalid maths | Number("abc") |
Infinity |
result of dividing a non-zero number by zero | 10 / 0 |
Loose equality == |
compares after converting types | 5 == "5" → true |
Strict equality === |
same value and same type | 5 === "5" → false |
| Logical operators | && AND, \|\| OR, ! NOT |
age >= 18 && hasTicket |
| Short-circuiting | stopping as soon as the answer is known | friends !== 0 && … |
| Truthy / falsy | values that act like true / false | "0" truthy, 0 falsy |
| Nullish | null or undefined |
let coupon; |
?? nullish coalescing |
default only for null/undefined | discount ?? 10 |
Summary
+ - * / % **;/keeps decimals;Math.floorfor whole shares;%for leftovers.- Precedence follows BODMAS: brackets first.
++,+=,-=,*=are shortcuts (they needlet).- Comparisons return
true/false. Always===and!==. +glues if either side is text; convert input withNumber(); check failures withNumber.isNaN().&&,||,!short-circuit; six falsy values; use??for defaults.
Next episode: strings and template literals. Building an order message with + gets messy, so you'll invent a cleaner way.