JavaScript Variables & Data Types
Variables values ko names ke saath store karne dete hain, aur data types batate hain ki kisi value ka nature kya hai. Is chapter me let, const, primitive types, typeof aur dynamic typing ko step by step samjhenge.
What is a variable?
A variable is a named binding that lets your program refer to a value. Instead of repeating a literal value everywhere, you can give it a meaningful name.
let studentName = "Broun";
let score = 90;
console.log(studentName);
console.log(score);Variable ko labelled box jaisa samjho. Box ka naam score hai aur uske andar current value 90 hai. Code me hum naam use karke value ko refer karte hain.
Declaration and assignment
Creating a variable name is called declaration. Giving it a value is assignment. Dono same line par bhi ho sakte hain.
let city; // declaration
city = "Delhi"; // assignment
let course = "JavaScript"; // declaration + assignmentlet
Use let when the variable needs to be reassigned later.
let progress = 20;
console.log(progress);
progress = 35;
console.log(progress);The variable still has the same name, but its current value changes.
const
Use const when the binding should not be reassigned after initialization.
const courseName = "JavaScript";
console.log(courseName);A const declaration must receive a value immediately, and assigning a new value to the same binding causes an error.
const means the binding cannot be reassigned. It does not automatically make every object or array stored inside it deeply immutable; objects and arrays will be covered later.What about var?
var is the older variable declaration keyword. It has function-scoping and hoisting behavior that differs from let and const.
var oldStyle = "works";
console.log(oldStyle);const rakho; jab reassignment genuinely needed ho tab let use karo. var ko legacy code samajhne ke liye learn karna useful hai.Variable naming rules
JavaScript identifiers follow naming rules.
- Name letter, underscore or dollar sign se start ho sakta hai.
- Digit se start nahi ho sakta.
- Letters and digits later positions me allowed hain.
- Reserved keywords variable names nahi ban sakte.
- Names case-sensitive hote hain.
let userName = "Aman";
let user2 = "Riya";
let _draft = true;
let $price = 499;x jaise vague names ke bajay cartTotal, studentName, isLoggedIn jaise names code ko readable banate hain.
camelCase convention
JavaScript variables commonly use camelCase: first word lowercase, later words capital letter se start.
let firstName = "Broun";
let courseProgress = 45;
let isCourseComplete = false;What are data types?
A data type describes the kind of value you are working with. JavaScript has primitive values and objects. In this chapter we focus on the primitive types first.
String
A string represents text. Strings can use single quotes, double quotes or template literals.
const first = "JavaScript";
const second = 'BrounStack';
const third = `Learning`;
console.log(first);Number
The number type represents both integer-like and floating-point numeric values.
const chapters = 18;
const rating = 4.8;
const negative = -10;
console.log(chapters);
console.log(rating);JavaScript also has special numeric values such as Infinity and NaN.
NaN
NaN means “Not-a-Number”, but its JavaScript type is still number. It commonly appears when a numeric operation cannot produce a meaningful numeric result.
const result = Number("hello");
console.log(result); // NaN
console.log(typeof result); // numberType conversion is covered properly in Chapter 4.
BigInt
bigint can represent integers beyond the safe integer range of regular number values. A simple BigInt literal ends with n.
const hugeValue = 9007199254740993n;
console.log(typeof hugeValue); // bigintnumber. BigInt is for specific integer requirements.Boolean
A boolean has only two values: true or false. Booleans are heavily used in conditions and state.
const isLoggedIn = true;
const isComplete = false;
console.log(isLoggedIn);undefined
undefined often means a value has not been assigned or is not available.
let nextLesson;
console.log(nextLesson); // undefined
console.log(typeof nextLesson); // undefinednull
null is an intentional “no value” marker used by developers and APIs when absence is explicit.
const selectedCourse = null;
console.log(selectedCourse);undefined often matlab value abhi assign nahi hui; null commonly matlab deliberately “no value” set ki gayi. Exact meaning project/API design par depend kar sakta hai.
Symbol
symbol creates unique primitive values, often used for specialized object keys and framework/library internals.
const idA = Symbol("id");
const idB = Symbol("id");
console.log(idA === idB); // falsetypeof operator
typeof returns a string describing the runtime type category of a value.
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof 10n); // bigint
console.log(typeof Symbol()); // symbolThe typeof null surprise
For historical compatibility, typeof null returns "object". This is a well-known legacy behavior; null itself is still a primitive value.
console.log(typeof null); // "object"typeof null === "object" ko dekhkar null ko object type mat samajhna.Dynamic typing
JavaScript is dynamically typed. A variable binding can later refer to a value of another type when reassignment is allowed.
let value = 25;
console.log(typeof value); // number
value = "twenty five";
console.log(typeof value); // stringThis flexibility is powerful, but changing types carelessly can make code harder to reason about.
Literal values
A literal is a value written directly in code.
"Hello" // string literal
42 // number literal
true // boolean literal
10n // bigint literal
null // null literalReassignment vs redeclaration
Reassignment changes the value of an existing let binding. Redeclaration tries to declare the same name again in the same scope.
let level = 1;
level = 2; // reassignment: allowed
// let level = 3; // same-scope redeclaration: errorBeginner best practices
- Default to
const; useletwhen reassignment is required. - Meaningful camelCase names use karo.
- Variable name se value ka purpose clear rakho.
- Unnecessary type-changing reassignment avoid karo.
varko new beginner code me default mat banao.typeof nulllegacy exception remember karo.- Values inspect karne ke liye console and
typeofuse karo.
Common beginner mistakes
constko baad me reassign karna.letaurconstka difference ignore karna.- Variable name digit se start karna.
- Reserved keyword ko identifier banana.
- String value ko quotes ke bina likhna.
undefinedaurnullko exactly same samajhna.NaNko separate JavaScript type samajhna.typeof nullresult ko literal truth maan lena.- Meaningless names jaise
a,b,x1everywhere use karna.
Chapter checklist
- Variable, declaration aur assignment explain kar sakte ho?
let,constaurvarka basic difference clear hai?- Valid variable names bana sakte ho?
- Primitive data types recognize kar sakte ho?
typeofuse karke value inspect kar sakte ho?undefined,null,NaNaur dynamic typing ka basic meaning clear hai?
Practice Task — Variable Lab
Browser Console me variables aur types ki practice karo.
const studentName,let scoreaurconst isActivevariables banao.scoreko new value se reassign karo.- Ek
constko reassign karke error observe karo; phir code fix karo. - String, number, boolean, undefined, null aur bigint values create karo.
- Har value ke saath
typeofprint karo. typeof nullka result note karo.Number("hello")run karkeNaNaur uskatypeofinspect karo.- Ek
let valueko number se string me reassign karke dynamic typing observe karo. - Three poor variable names ko clear camelCase names me rewrite karo.
- Exercises dobara bina copy-paste ke khud type karo.