By WebNest Studio Engineering Team
JavaScript Tutorial
JavaScript Data Types
Every value in JavaScript has a type, and JavaScript divides its types into two broad categories: primitive types and objects. Primitives are simple, immutable values compared by their actual value, while objects are more complex structures compared by reference.
JavaScript is also a dynamically typed language, meaning a variable's type is determined by the value it currently holds, not fixed at declaration — the same variable can hold a number at one point and a string later, and the engine adapts automatically.
Why it matters
JavaScript converts between types automatically, and the results are sometimes surprising: "5" + 1 is "51" but "5" - 1 is 4. Add the fact that typeof null reports "object", and type checks become a common source of bugs. Knowing the types and using strict equality avoids most of them.
The Seven Primitive Types
JavaScript has seven primitive types, each representing an immutable value.
string— textual data, written in single quotes, double quotes, or backticks.number— both integers and floating-point numbers share this single type (e.g. 42, 3.14, -7).boolean— eithertrueorfalse.null— represents the intentional absence of any value, typically set explicitly.undefined— represents a variable that has been declared but not yet assigned a value.symbol— a unique, unforgeable value often used as an object property key.bigint— represents integers of arbitrary size beyond the safe range of number, written with an "n" suffix (e.g. 123n).
Objects: Everything Else
Anything that is not one of the seven primitives is an object, including plain objects ({}), arrays, functions, dates, and regular expressions. Objects are mutable and are stored and compared by reference: two separately created objects with identical contents are not considered equal with ===.
Checking Types with typeof
The typeof operator returns a string identifying a value's type, such as "string", "number", or "object". It has one famous quirk: typeof null returns "object", a long-standing bug in the language that cannot be fixed without breaking existing code. To check specifically for an array, use Array.isArray(value) instead of typeof.
Equality: == versus ===
=== (strict equality) compares both value and type without converting either operand, and is the recommended default. == (loose equality) first performs type coercion if the operands have different types, which can produce surprising results, such as 0 == false being true, or "" == 0 being true. Using === and !== consistently avoids these coercion surprises.
Examples
typeof across primitives and objects, including the null quirk
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null); // famous quirk
console.log(typeof Symbol("id"));
console.log(typeof 10n);
console.log(typeof { a: 1 });
console.log(typeof [1, 2, 3]); // arrays report "object" too
console.log(Array.isArray([1, 2, 3]));
string
number
boolean
undefined
object
symbol
bigint
object
object
true
Loose equality coercion versus strict equality
console.log(0 == false);
console.log(0 === false);
console.log("" == 0);
console.log(null == undefined);
console.log(null === undefined);
console.log(NaN === NaN); // NaN is never equal to itself
true
false
true
true
false
false
Common Mistakes
- Assuming "typeof null" returns "null" — it actually returns "object", a legacy quirk of the language.
- Using "==" out of habit and being surprised by coercion results like "0 == false" or "'' == 0" being true.
- Comparing two objects or arrays with identical contents using "===" and expecting true — reference types compare by identity, not by content.
- Forgetting that "NaN === NaN" is false, and using "value === NaN" to test for NaN instead of "Number.isNaN(value)".
Exercise
typeof reports "object" for plain objects, for arrays and for null. Write isPlainObject, which returns true only for a real object that is neither null nor an array.
Expected output: true, false, false, false (one per line)
function isPlainObject(value) {
// TODO: true only for a non-null object that is not an array
}
console.log(isPlainObject({ name: "Asha" }));
console.log(isPlainObject(null));
console.log(isPlainObject([1, 2, 3]));
console.log(isPlainObject("text"));
Show hints
- Check
typeof value === "object"first, then rule outnullwithvalue !== null. Array.isArray(value)is the reliable test for an array.
Show solution
function isPlainObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
console.log(isPlainObject({ name: "Asha" }));
console.log(isPlainObject(null));
console.log(isPlainObject([1, 2, 3]));
console.log(isPlainObject("text"));
Quick quiz
1. What does typeof null return?
- A. "null"
- B. "undefined"
- C. "boolean"
- D. "object"
Show answer
D. It is a long-standing quirk of the language. Test for null with value === null.
2. What is the result of "5" + 1?
- A. "51"
- B. NaN
- C. A TypeError
- D. 6
Show answer
A. With +, if either operand is a string the other is converted to a string and they are joined.
3. What does 0 == "" evaluate to, and 0 === ""?
- A. false, then false
- B. true, then false
- C. true, then true
- D. false, then true
Show answer
B. == converts the empty string to 0 before comparing. === compares without converting.
Interview questions
What is the difference between == and ===?
Show answer
===, strict equality, is true only when both operands have the same type and the same value. ==, loose equality, first converts the operands to a common type, which gives results such as "1" == 1 and null == undefined being true. Because those conversions are hard to remember, === is used almost always. Objects are compared by reference with both operators.
What is the difference between null and undefined?
Show answer
undefined is what JavaScript gives a variable that has been declared but not assigned, a missing property, or a function that returns nothing. null is a value a programmer assigns on purpose to mean "no value". They are loosely equal to each other but not strictly equal, and typeof returns "undefined" for one and "object" for the other.
Key Points to Remember
- JavaScript has seven primitive types: string, number, boolean, null, undefined, symbol, and bigint; everything else is an object.
- typeof identifies primitive types reliably except for the historical "typeof null === 'object'" quirk.
- === compares value and type with no coercion; == coerces types first and can produce surprising results.
- Objects (including arrays and functions) are compared by reference, not by their contents.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering