Variables and Datatypes

Variables :
Whenever we talk about variables in any language the first thought that comes in our mind that "Variables are that values which we hold in the memory". But nobody questions that why we hold these variables in any memory location. So the answer is that variables are very basic bulding block for any programming language. All the operations are donw with the help of variables.
Why we use variables?
Variables are fundamental in programming because they act as named containers to store, access, and manipulate data efficiently. They make code more readable, reusable, and adaptable by letting you reference values symbolically instead of hardcoding them repeatedly.
Core Purpose
Variables hold data like numbers, text, or complex structures in memory, allowing programs to remember and change values dynamically. Without them, you'd rewrite the same literal values everywhere, making code rigid and error-prone.
How to declare variables using var, let, and const ?
JavaScript offers three keywords—var, let, and const—to declare variables, each with distinct scoping, mutability, and hoisting behaviors for better code control.
Declaration Syntax
Use var for older function-scoped variables, let for block-scoped changeable values, and const for block-scoped constants that can't be reassigned. Basic form: keyword name = value;, like let age = 20;.
Key Differences
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function/global | Block | Block |
| Reassignment | Yes | Yes | No |
| Redeclaration | Yes | No | No |
| Hoisting | Yes (undefined) | Yes (TDZ error) | Yes (TDZ error) |
Usage Examples
var globalVar = 1; // Function/global scope, reassignable
let changeable = 2; // Block scope, reassignable: changeable = 3;
const fixed = 4; // Block scope, fixed value
if (true) {
let blockLet = 5; // Only accessible inside block
const blockConst = 6; // Can't reassign inside block
}
console.log(blockLet); // Error: not defined
Prefer let for values that update (e.g., counters in web apps) and const for unchanging data like API endpoints you're building with.
The one term we discuss above is "SCOPE". So what actually a scope is?
Scope in JavaScript is like a "private room" rule—it decides where your variables can be seen and used in your code, keeping things organized and safe.
Global Scope
Think of this as the whole house. Variables declared here (without let, var, or const outside functions) are accessible everywhere in your script. Example: let globalName = "Purnima";—you can use it anywhere.
Function Scope
This is like a room inside the house. Variables inside a function (using var) stay locked in that function. Example:
function greet() {
let roomVar = "Hello"; // Only works inside greet()
console.log(roomVar);
}
greet(); // Works
console.log(roomVar); // Error! Locked outside
Perfect for private calculations in your web functions.
Block Scope
Newer and stricter—like a closet inside the room. let and const limit variables to {} blocks (if, for loops). Example:
if (true) {
let closetVar = "Secret"; // Only inside this if block
}
console.log(closetVar); // Error! Can't escape the block
Use this in loops for your JS projects to avoid bugs.
This builds on var/let/const—var ignores blocks, but let/const respect them for cleaner web code you're learning.
Datatypes :
Data types define the kind of data a variable can hold in JavaScript, such as text, numbers, or true/false values, ensuring proper operations and memory use.
There are two types of Datatypes :
Primitive Datatypes
Non - Primitive Datatypes
Primitive Datatpes :
Primitive data types in JavaScript are the simplest, immutable values that represent single pieces of data, passed by value and stored directly in memory.
String
String holds sequences of characters for text, enclosed in single, double quotes, or backticks. Example: let name = "Purnima"; checks as typeof name returning "string". Use it for names, messages in your web projects.
Number
Number covers all numeric values, including integers and decimals (floating-point). Example: let age = 20; or let pi = 3.14;, with typeof as "number". Ideal for calculations like scores or dimensions.
BigInt
BigInt manages arbitrarily large integers beyond Number's safe limit, suffixed with 'n'. Example: let bigNum = 12345678901234567890n;. Great for precise large-number math in apps.
Boolean
Boolean represents logical true or false only. Example: let isActive = true;, useful for conditions like if (isStudent) { ... }. typeof returns "boolean", key for decision-making in JS code.
Undefined
Undefined means a variable declared but not assigned a value yet. Example: let score; console.log(score); outputs undefined. typeof confirms "undefined", common in uninitialized vars.
Null
Null explicitly indicates no value or empty object reference. Example: let user = null;. Despite typeof null wrongly showing "object", it's primitive for clearing data intentionally.
Symbol
Symbol creates unique, immutable identifiers, often for object keys to avoid collisions. Example: let id = Symbol('user');. typeof is "symbol", helpful in advanced web dev for private properties.
Non Primitive Datatypes :
Non-primitive data types in JavaScript, also called reference types, are complex structures that store collections of data and are mutable, unlike simple primitives.
Object
Object is the main non-primitive type, acting as a collection of key-value pairs for unstructured data. Example: let student = {name: "Purnima", age: 20};—access with student.name or student["age"]. Use typeof returns "object"; ideal for configs in web apps.
Array
Array holds ordered lists of values (any type) using square brackets and indexes starting at 0. Example: let skills = ["HTML", "CSS", "JS"];—access skills[0] gives "HTML". Length via skills.length; great for lists like menu items.
Function
Functions are special objects that execute code, declared with function or arrows. Example: function greet(name) { return "Hi, " + name; } or const add = (a, b) => a + b;. Callable via greet("Purnima"); essential for event handlers in your JS learning.
These are stored by reference (shared memory), so changes affect copies—key for dynamic web development.


