LearningJavaScript TutorialVariables & Data Types
CHAPTER 3 · VALUES

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.

English + Hinglish32 min readPractice included

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.

variables.jsJS
let studentName = "Broun";
let score = 90;

console.log(studentName);
console.log(score);
Hinglish Explanation

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.

declaration.jsJS
let city;          // declaration
city = "Delhi";   // assignment

let course = "JavaScript"; // declaration + assignment

let

Use let when the variable needs to be reassigned later.

let.jsJS
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.jsJS
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.

Important: 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.

legacy previewJS
var oldStyle = "works";
console.log(oldStyle);
Beginner recommendation: New code me default choice 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.
names.jsJS
let userName = "Aman";
let user2 = "Riya";
let _draft = true;
let $price = 499;
Good naming

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.

camel-case.jsJS
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.

strings.jsJS
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.

numbers.jsJS
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.

nan.jsJS
const result = Number("hello");

console.log(result);        // NaN
console.log(typeof result); // number

Type 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.

bigint.jsJS
const hugeValue = 9007199254740993n;
console.log(typeof hugeValue); // bigint
Beginner note: Most everyday counters, prices and measurements use number. BigInt is for specific integer requirements.

Boolean

A boolean has only two values: true or false. Booleans are heavily used in conditions and state.

booleans.jsJS
const isLoggedIn = true;
const isComplete = false;

console.log(isLoggedIn);

undefined

undefined often means a value has not been assigned or is not available.

undefined.jsJS
let nextLesson;

console.log(nextLesson);        // undefined
console.log(typeof nextLesson); // undefined

null

null is an intentional “no value” marker used by developers and APIs when absence is explicit.

null.jsJS
const selectedCourse = null;
console.log(selectedCourse);
undefined vs null

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.

symbol.jsJS
const idA = Symbol("id");
const idB = Symbol("id");

console.log(idA === idB); // false
For now: Symbol ko recognize karna enough hai. Object-oriented use cases later chapters me easier lagenge.

typeof operator

typeof returns a string describing the runtime type category of a value.

typeof.jsJS
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());  // symbol

The typeof null surprise

For historical compatibility, typeof null returns "object". This is a well-known legacy behavior; null itself is still a primitive value.

legacy behaviorJS
console.log(typeof null); // "object"
Remember: 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.

dynamic.jsJS
let value = 25;
console.log(typeof value); // number

value = "twenty five";
console.log(typeof value); // string

This 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.

literals.jsJS
"Hello"   // string literal
42        // number literal
true      // boolean literal
10n       // bigint literal
null      // null literal

Reassignment vs redeclaration

Reassignment changes the value of an existing let binding. Redeclaration tries to declare the same name again in the same scope.

reassignment.jsJS
let level = 1;
level = 2; // reassignment: allowed

// let level = 3; // same-scope redeclaration: error

Beginner best practices

  • Default to const; use let when reassignment is required.
  • Meaningful camelCase names use karo.
  • Variable name se value ka purpose clear rakho.
  • Unnecessary type-changing reassignment avoid karo.
  • var ko new beginner code me default mat banao.
  • typeof null legacy exception remember karo.
  • Values inspect karne ke liye console and typeof use karo.

Common beginner mistakes

  • const ko baad me reassign karna.
  • let aur const ka difference ignore karna.
  • Variable name digit se start karna.
  • Reserved keyword ko identifier banana.
  • String value ko quotes ke bina likhna.
  • undefined aur null ko exactly same samajhna.
  • NaN ko separate JavaScript type samajhna.
  • typeof null result ko literal truth maan lena.
  • Meaningless names jaise a, b, x1 everywhere use karna.

Chapter checklist

  • Variable, declaration aur assignment explain kar sakte ho?
  • let, const aur var ka basic difference clear hai?
  • Valid variable names bana sakte ho?
  • Primitive data types recognize kar sakte ho?
  • typeof use karke value inspect kar sakte ho?
  • undefined, null, NaN aur dynamic typing ka basic meaning clear hai?

Practice Task — Variable Lab

Browser Console me variables aur types ki practice karo.

  1. const studentName, let score aur const isActive variables banao.
  2. score ko new value se reassign karo.
  3. Ek const ko reassign karke error observe karo; phir code fix karo.
  4. String, number, boolean, undefined, null aur bigint values create karo.
  5. Har value ke saath typeof print karo.
  6. typeof null ka result note karo.
  7. Number("hello") run karke NaN aur uska typeof inspect karo.
  8. Ek let value ko number se string me reassign karke dynamic typing observe karo.
  9. Three poor variable names ko clear camelCase names me rewrite karo.
  10. Exercises dobara bina copy-paste ke khud type karo.