📘 Lesson · Lesson 07
JavaScript Data Types
JavaScript Data Types
What Data Types Are
A data type describes what KIND of value a variable holds — text, a number, a true/false answer, and so on. JavaScript cares about types because different types behave differently: you can add two numbers, but "adding" two pieces of text glues them together instead. Importantly, JavaScript is dynamically typed: you never declare a type, and a variable can hold a number today and text tomorrow. The type is decided by the VALUE you put in, not by you. Understanding the types is essential because most beginner bugs come from a value not being the type you assumed.
The Primitive Types
let name = "Priya"; // string - text, in quotes
let age = 20; // number - integers or decimals
let isStudent = true; // boolean - only true or false
let city; // undefined - declared but no value yet
let score = null; // null - intentionally "no value"
| Type | Holds | Example |
|---|---|---|
| string | Text | "Aman", 'Hello', "123" |
| number | Any number | 42, -7, 3.14 |
| boolean | true or false only | true, false |
| undefined | No value assigned yet | let x; |
| null | Deliberately empty | let x = null; |
The three types you'll use constantly are
string, number, and boolean. A string is text wrapped in quotes — single or double, both work. A number covers whole numbers AND decimals (unlike some languages, JavaScript has just one number type). A boolean holds only true or false, which powers all decision-making in code. Note: "123" in quotes is a STRING, not a number — a distinction that causes many bugs. These simple types are called "primitives" because they hold a single, simple value.null vs undefined — a classic confusion
let a; // undefined — you declared it, but never gave it a value
let b = null; // null — you DELIBERATELY set it to "empty"
Both mean "no value," but the difference is intent.
undefined is what JavaScript gives a variable automatically when you declare it without assigning anything — it means "nothing here yet." null is a value YOU assign on purpose to say "this is intentionally empty." A useful analogy: undefined is an empty box you haven't filled yet; null is a box you deliberately emptied and labelled "empty." In practice you'll see undefined appear on its own, while you use null deliberately to clear a value. This appears in interviews often, so remember: undefined = not set; null = set to nothing.Objects and Arrays — the complex types
// Array - an ordered list of values (square brackets):
let marks = [85, 92, 78];
// Object - a collection of named properties (curly braces):
let student = {
name: "Aman",
rollNo: 12,
passed: true
};
Beyond primitives, JavaScript has
object — a type that holds a COLLECTION of values rather than a single one. Two forms matter most: an array (a numbered list, written with [ ]) holds many values in order — like a list of a student's marks. An object (written with { }) holds named properties — like a student record with a name, roll number, and pass status. These let you model real-world data properly, and you'll spend two whole chapters on each later. For now, just recognise them: [ ] = array, { } = object.The typeof Operator — check any type
console.log(typeof "Aman"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof {name:"A"}); // "object"
console.log(typeof null); // "object" ← a famous JavaScript BUG!
typeof tells you the type of any value — invaluable when debugging ("is this actually a number, or a string that LOOKS like a number?"). Just write typeof followed by the value or variable. One famous quirk: typeof null returns "object", which is wrong — it's a bug from JavaScript's earliest days that can never be fixed without breaking millions of websites. It's a classic interview trivia question. Apart from that oddity, typeof is reliable and a handy companion to console.log when a value isn't behaving as you expected.Exam Corner
Q: Name the primitive data types in JavaScript. string, number, boolean, undefined, null (also symbol and bigint).
Q: What does "dynamically typed" mean? You don't declare types; a variable's type is set by the value assigned, and can change.
Q: Difference between null and undefined? undefined = declared but never assigned; null = deliberately set to "no value".
Q: What does typeof do? Returns the data type of a value as a string.
Q: What does typeof null return, and why is it notable? "object" — a long-standing bug in JavaScript that can't be fixed.
Q: What does "dynamically typed" mean? You don't declare types; a variable's type is set by the value assigned, and can change.
Q: Difference between null and undefined? undefined = declared but never assigned; null = deliberately set to "no value".
Q: What does typeof do? Returns the data type of a value as a string.
Q: What does typeof null return, and why is it notable? "object" — a long-standing bug in JavaScript that can't be fixed.
Data Types क्या हैं
Data type बताता है variable किस KISM का value रखता है — text, number, true/false जवाब, वगैरह. JavaScript types की परवाह करता है क्योंकि अलग types अलग व्यवहार करते हैं: आप दो numbers जोड़ सकते हैं, पर दो text टुकड़ों को "जोड़ना" उन्हें आपस में चिपका देता है. अहम बात, JavaScript dynamically typed है: आप कभी type declare नहीं करते, और variable आज number रख सकता है और कल text. Type उस VALUE से तय होता है जो आप डालते हैं, आपसे नहीं. Types समझना ज़रूरी है क्योंकि ज़्यादातर beginner bugs इससे आते हैं कि value वह type नहीं जो आपने मान लिया.
Primitive Types
let name = "Priya"; // string - text, quotes me
let age = 20; // number - integers ya decimals
let isStudent = true; // boolean - sirf true ya false
let city; // undefined - declared par abhi koi value nahi
let score = null; // null - jaanbujhkar "koi value nahi"
| Type | रखता है | Example |
|---|---|---|
| string | Text | "Aman", 'Hello', "123" |
| number | कोई भी number | 42, -7, 3.14 |
| boolean | सिर्फ true या false | true, false |
| undefined | अभी कोई value assign नहीं | let x; |
| null | जानबूझकर खाली | let x = null; |
तीन types जो आप लगातार use करेंगे:
string, number, और boolean. String quotes में लिपटा text है — single या double, दोनों चलते हैं. Number पूर्ण संख्याएं AUR decimals दोनों cover करता है (कुछ languages के उलट, JavaScript में सिर्फ एक number type है). Boolean सिर्फ true या false रखता है, जो code में सारे फैसले चलाता है. ध्यान: quotes में "123" STRING है, number नहीं — यह भेद कई bugs पैदा करता है. ये सरल types "primitives" कहलाते हैं क्योंकि वे एकल, सरल value रखते हैं.null vs undefined — classic उलझन
let a; // undefined — declare kiya, par kabhi value nahi di
let b = null; // null — aapne JAANBUJHKAR "khali" set kiya
दोनों का मतलब "कोई value नहीं," पर फर्क इरादे का है.
undefined वह है जो JavaScript variable को अपने आप देता है जब आप बिना कुछ assign किए declare करें — मतलब "अभी यहां कुछ नहीं." null वह value है जो AAP जानबूझकर assign करते हैं कहने को "यह जानबूझकर खाली है." उपयोगी analogy: undefined खाली box है जिसे आपने भरा नहीं; null वह box है जिसे आपने जानबूझकर खाली करके "empty" label किया. व्यवहार में आप undefined को खुद आते देखेंगे, जबकि null जानबूझकर value साफ करने को use करते हैं. यह interviews में अक्सर आता है, तो याद रखिए: undefined = set नहीं; null = कुछ नहीं पर set.Objects और Arrays — complex types
// Array - values ki kramik list (square brackets):
let marks = [85, 92, 78];
// Object - named properties ka sangrah (curly braces):
let student = {
name: "Aman",
rollNo: 12,
passed: true
};
Primitives के अलावा, JavaScript में
object है — वह type जो एकल के बजाय values का SANGRAH रखता है. दो रूप सबसे अहम: array (numbered list, [ ] से लिखा) कई values क्रम में रखता है — जैसे student के marks की list. Object ({ } से लिखा) named properties रखता है — जैसे student record जिसमें नाम, roll number, और pass status हो. ये आपको असली दुनिया का data ठीक से model करने देते हैं, और आप बाद में हर एक पर दो पूरे chapters बिताएंगे. अभी बस पहचानिए: [ ] = array, { } = object.typeof Operator — कोई भी type जांचिए
console.log(typeof "Aman"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof {name:"A"}); // "object"
console.log(typeof null); // "object" ← mashhoor JavaScript BUG!
typeof आपको किसी भी value का type बताता है — debugging में अमूल्य ("क्या यह असल में number है, या string जो number जैसा DIKHTA है?"). बस typeof लिखिए उसके बाद value या variable. एक मशहूर quirk: typeof null "object" लौटाता है, जो गलत है — यह JavaScript के शुरुआती दिनों का bug है जो लाखों websites तोड़े बिना कभी ठीक नहीं हो सकता. यह classic interview trivia सवाल है. उस अजीबता के अलावा, typeof भरोसेमंद है और console.log का काम का साथी जब value आपकी उम्मीद के मुताबिक न चले.Exam Corner
Q: JavaScript में primitive data types बताइए. string, number, boolean, undefined, null (साथ ही symbol और bigint).
Q: "Dynamically typed" का क्या मतलब? आप types declare नहीं करते; variable का type assign किए value से तय होता है, और बदल सकता है.
Q: null और undefined में अंतर? undefined = declared पर कभी assign नहीं; null = जानबूझकर "कोई value नहीं" set किया.
Q: typeof क्या करता है? Value का data type string के रूप में लौटाता है.
Q: typeof null क्या लौटाता है, और यह उल्लेखनीय क्यों? "object" — JavaScript का पुराना bug जो ठीक नहीं हो सकता.
Q: "Dynamically typed" का क्या मतलब? आप types declare नहीं करते; variable का type assign किए value से तय होता है, और बदल सकता है.
Q: null और undefined में अंतर? undefined = declared पर कभी assign नहीं; null = जानबूझकर "कोई value नहीं" set किया.
Q: typeof क्या करता है? Value का data type string के रूप में लौटाता है.
Q: typeof null क्या लौटाता है, और यह उल्लेखनीय क्यों? "object" — JavaScript का पुराना bug जो ठीक नहीं हो सकता.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.