- A variable is a named storage location in computer memory
- Used to store and manage data during program execution
- Acts as a container that holds a specific value which can be modified or referenced
- Provides a way to label and store data
- Enables dynamic data manipulation
- Supports various data types and scopes
var oldStyleVariable = "I'm an old school variable";- Function-scoped or globally-scoped
- Can be re-declared and updated
- Hoisted to the top of its scope
- Less predictable behavior
- Not Recommended in Modern JavaScript
console.log(x); // Outputs: undefined (not an error)
var x = 5;let modernVariable = "I'm a block-scoped variable";- Block-scoped (limited to {} block)
- Can be updated but not re-declared in same scope
- More predictable behavior
- Prevents unintended global variable creation
- Recommended for Mutable Variables
{
let blockVariable = "I exist only in this block";
}
// blockVariable is not accessible outside the blockconst unchangeableValue = "I cannot be reassigned";- Block-scoped
- Cannot be reassigned after initial value
- Prevents accidental variable reassignment
- Recommended as Default Declaration
const person = { name: "Pankaj" };
person.name = "Updated Name"; // Allowed
// person = {}; // This would cause an error- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain letters, numbers, underscores, dollar signs
- Case-sensitive (
ageandAgeare different variables) - No spaces or special characters allowed
- Use camelCase for variable names
- Choose descriptive and meaningful names
- Avoid single-letter names (except in specific contexts like loops)
let firstName = "Pankaj";
let totalStudentCount = 100;
let isLoggedIn = true;let 2name = "Invalid"; // Starts with number
let user-name = "Not Good"; // Contains hyphen
let class = "Reserved word"; // Reserved keyword-
String: Text data
let greeting = "Hello, World!";
-
Number: Numeric values
let age = 30; let temperature = 98.6;
-
Boolean: True/False values
let isStudent = true;
-
Undefined: Declared but not assigned
let undefinedVar;
-
Null: Intentional absence of any object value
let emptyValue = null;
-
Symbol: Unique identifier (ES6)
let uniqueKey = Symbol('description');
-
BigInt: For very large integers
let bigNumber = 1234567890123456789012345678901234567890n;
-
Object: Key-value collections
let person = { name: "Pankaj", age: 30, skills: ["JavaScript", "React"] };
-
Array: Ordered list of values
let fruits = ["Apple", "Banana", "Cherry"];
- Variables declared outside any function
- Accessible everywhere in the code
- Variables declared inside a function
- Accessible only within that function
- Variables declared inside {} blocks
- Limited accessibility
let globalVar = "Global";
function exampleScope() {
let functionVar = "Function Scope";
if (true) {
let blockVar = "Block Scope";
console.log(globalVar); // Accessible
console.log(functionVar); // Accessible
console.log(blockVar); // Accessible
}
// console.log(blockVar); // Would cause an error
}- Use
constby default - Use
letwhen value will change - Avoid
varin modern JavaScript - Initialize variables when declaring
- Group related declarations together
- Declare variables as close to usage as possible
- Release references when no longer needed
- Be mindful of variable lifetime
let result = "5" + 3; // Becomes "53" (string concatenation)
let math = "5" - 3; // Becomes 2 (numeric subtraction)let stringNum = "100";
let number = Number(stringNum); // Converts to number
let string = String(number); // Converts to string
let boolean = Boolean(stringNum); // Converts to booleanUnderstanding variables is fundamental to JavaScript programming. Practice and consistent use will make these concepts second nature.
const: Unchanging valueslet: Changing valuesvar: Avoid in modern code
Happy Coding! 🚀👨💻