Values, Types & The Coercion Trap
JS-এর ৮টি type এবং coercion-এর ফাঁদ
1. The 8 Types of JavaScript
Every JavaScript value belongs to exactly one of 8 types. Seven are primitives (immutable, copied by value); the eighth is the catch-all object.
| Type | Example | Notes |
|---|---|---|
string | "হ্যালো" | UTF-16 text |
number | 42, 3.14, NaN | IEEE 754 64-bit float |
boolean | true, false | Two values only |
null | null | Intentional absence |
undefined | undefined | Default for unset variables |
symbol | Symbol("id") | Unique identifier (rare in beginner code) |
bigint | 9007199254740993n | Arbitrary-precision integers |
object | {}, [], functions | Everything non-primitive |
2. The typeof Operator
typeof returns a string describing the type. It has one famous bug from 1995 that has never been fixed: typeof null === "object".
console.log(typeof "hi"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ← legacy bug
console.log(typeof {}); // "object"
console.log(typeof []); // "object" (Array.isArray helps)
console.log(typeof console.log); // "function"
console.log(typeof 10n); // "bigint"
console.log(typeof Symbol("id")); // "symbol"
Array.isArray(x), never typeof x === "array" (no such type) or x instanceof Array (fails across iframes).
3. Coercion — Implicit vs Explicit
JS converts types automatically when an operator demands it. This is coercion, and it is the #1 source of beginner bugs.
console.log(1 + "2"); // "12" number → string
console.log("5" - 2); // 3 string → number
console.log("5" * "2"); // 10
console.log(true + 1); // 2 true → 1
console.log(false + "!"); // "false!"
console.log(null + 1); // 1 null → 0
console.log(undefined + 1); // NaN undefined → NaN
console.log([] + []); // ""
console.log([] + {}); // "[object Object]"
console.log(Number("42")); // 42
console.log(Number("4.5")); // 4.5
console.log(Number("hi")); // NaN
console.log(String(3.14)); // "3.14"
console.log(Boolean(0)); // false
console.log(Boolean("hi")); // true
console.log(parseInt("42px")); // 42
console.log(parseFloat("3.14m")); // 3.14
4. == vs === — The Most Important Rule
== coerces before comparing. === never coerces — types must match. The community-wide rule for 2026: always use ===, with one rare exception (x == null to catch both null and undefined).
console.log(0 == ""); // true ← scary
console.log(0 == false); // true
console.log("" == false); // true
console.log(null == undefined); // true
console.log(1 == "1"); // true
console.log(0 === ""); // false ← sane
console.log(0 === false); // false
console.log(null === undefined);// false
console.log(1 === "1"); // false
"eqeqeq": "error" and never look back. ==-related bugs cost the JS world millions of hours per year.
5. NaN & Infinity
NaN stands for "Not a Number" — yet its type is number. It is the result of any nonsensical numeric operation.
console.log(0 / 0); // NaN
console.log(Number("abc")); // NaN
console.log(typeof NaN); // "number"
console.log(NaN === NaN); // false ← NaN ≠ NaN
console.log(Number.isNaN(NaN)); // true ← correct way
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(Number.isFinite(42)); // true
NaN মানে Not a Number হলেও এর type "number"। এটি নিজেও নিজের সমান নয়। চেক করতে Number.isNaN(x) ব্যবহার করুন।6. Primitives Are Copied by Value, Objects by Reference
let a = 5;
let b = a; // copy of 5
b = 99;
console.log(a, b); // 5 99
const obj1 = { name: "Arif" };
const obj2 = obj1; // same reference!
obj2.name = "Nusrat";
console.log(obj1.name); // "Nusrat" ← changed!
7. Glossary (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Primitive | Immutable basic value — string, number, boolean, null, undefined, symbol, bigint. | অপরিবর্তনীয় মৌলিক value — সাতটি প্রাথমিক type। |
| Coercion | Automatic type conversion when an operator demands it. | Operator-এর প্রয়োজনে স্বয়ংক্রিয় type পরিবর্তন। |
typeof | Operator that returns a string naming the type of its operand. | যেকোনো value-র type-এর নাম string হিসেবে দেয়। |
NaN | "Not a Number" — result of invalid math; type is number. | Not a Number — অবৈধ math-এর ফলাফল; type হলেও "number"। |
=== | Strict equality — compares value and type, no coercion. | কঠোর সাম্য — type ও value দুটোই মেলায়। |
== | Loose equality — coerces before comparing. Avoid. | শিথিল সাম্য — coerce করে compare করে। এড়িয়ে চলুন। |
| BigInt | Arbitrary-precision integer type, written with n suffix. | যেকোনো বড় integer ধরে রাখার type — শেষে n লাগে। |
| Reference | Pointer to an object in memory; copies share the same data. | Memory-তে object-এর pointer; copy-গুলো একই data share করে। |
typeof null === "object" — ১৯৯৫ সালের bug। typeof []-ও "object" — array চেক করতে Array.isArray() ব্যবহার করুন। সবসময় ===; শুধু x == null-এ == grant করা যায় (null + undefined একসাথে চেক)। Primitive value copy হয়; object reference copy হয়।
8. Practice Problems
-
Predict the output, then run:
console.log(typeof null);✨ Show Answer
Output:
"object"— a 1995 bug preserved for backward compatibility. To check for null, usex === null. -
Predict, then run:
"5" + 3and"5" - 3. Why are they different?✨ Show Answer
ans2.jsconsole.log("5" + 3); // "53" console.log("5" - 3); // 2+string-এর সাথে concat করে;-শুধু সংখ্যায় কাজ করে, তাই string-কে number-এ coerce করে। -
List all 6 falsy values in JavaScript and verify them with
Boolean().✨ Show Answer
ans3.js[false, 0, "", null, undefined, NaN] .forEach(v => console.log(v, "→", Boolean(v))); -
Why does
NaN === NaNreturnfalse? How should we test for NaN?✨ Show Answer
Answer: The IEEE 754 spec defines NaN as never-equal-to-anything, including itself, so any test against NaN with
===returnsfalse. UseNumber.isNaN(x)— it returnstrueonly for the genuine NaN value.IEEE 754 specification অনুসারে NaN কখনো নিজের সমান নয়। চেক করতে
Number.isNaN(x)ব্যবহার করুন। -
Show the result of
1 == "1",1 === "1",null == undefined,null === undefined.✨ Show Answer
ans5.jsconsole.log(1 == "1"); // true console.log(1 === "1"); // false console.log(null == undefined); // true console.log(null === undefined);// false -
Convert the string
"42"to a number using three different methods.✨ Show Answer
ans6.jsconsole.log(Number("42")); // 42 console.log(parseInt("42", 10)); // 42 console.log(+"42"); // 42 (unary +) -
Why does
typeof []return"object"? How would you correctly check for an array?✨ Show Answer
Arrays are objects under the hood —
typeofonly knows the broad category. UseArray.isArray(x)for a reliable check.ans7.jsconsole.log(typeof []); // "object" console.log(Array.isArray([])); // true console.log(Array.isArray({})); // false -
Demonstrate that primitive copy is by value, but object copy is by reference.
✨ Show Answer
ans8.jslet p1 = 10; let p2 = p1; p2 = 99; console.log("primitive:", p1, p2); // 10 99 const o1 = { x: 1 }; const o2 = o1; o2.x = 99; console.log("object :", o1.x, o2.x); // 99 99 -
Predict the output:
[] + [],[] + {},{} + [].✨ Show Answer
[] + []→""(both coerce to empty string).[] + {}→"[object Object]".{} + []can be0or"[object Object]"depending on whether{}is parsed as a block (in REPL) or an object (in expression).এই কারণে JS-এ পেশাদার কোডে কখনো এমন expression লিখবেন না। সর্বদা cast করুন বা
JSON.stringifyব্যবহার করুন। -
Use
Number.isIntegerto verify three test values:5,5.5, and"5".✨ Show Answer
ans10.jsconsole.log(Number.isInteger(5)); // true console.log(Number.isInteger(5.5)); // false console.log(Number.isInteger("5")); // false (strict) -
Build a function
type(x)that returns"array","null", ortypeof x— fixing the two famous bugs.✨ Show Answer
ans11.jsfunction type(x) { if (x === null) return "null"; if (Array.isArray(x)) return "array"; return typeof x; } console.log(type(null)); // "null" console.log(type([])); // "array" console.log(type(42)); // "number" console.log(type("hi")); // "string" -
In one paragraph, explain why
===should be your default and when==is acceptable.✨ Show Answer
Answer:
===compares both type and value, so it never lies.==performs implicit coercion using a 12-step algorithm few developers fully memorise — and the surprises (0 == "",[] == false) cause real production bugs. The single defensible use of==is the idiomx == null, which catches bothnullandundefinedin one expression. Outside that, configure your linter to forbid==.===type ও value দুটোই মেলায়, তাই কখনো ভুল করে না।==১২-ধাপের coercion algorithm চালায় যা মনে রাখা কঠিন। শুধুx == nullidiom-টি গ্রহণযোগ্য।
Summary — Module 04
JavaScript has 8 types: 7 primitives + object. typeof null wrongly returns "object"; typeof [] returns "object" too — use Array.isArray. Coercion is automatic — + with a string concatenates; everything else converts to numbers. Always use ===; the only exception is x == null. Primitives copy by value; objects (including arrays) copy by reference.
typeof-এর দুটি বিখ্যাত bug মনে রাখুন। সবসময় ===। Primitive value copy হয়, object reference copy হয়।