JavaScript Literals
JavaScript mein literal ka simple matlab hota hai:
koi bhi value jo aap directly code mein likhte ho (without calculation or variable reference)
let x = 42; // 42 is a numeric literal
let name = "Raj"; // "Raj" is a string literal
Rule:
Direct value = literal
No computation
No function needed
Types of JavaScript Literals
1. Numeric Literals
Numbers ko different formats mein likh sakte ho:
let int = 25;
let float = 99.99;
let hex = 0xFF; // 255
let binary = 0b1010; // 10
let octal = 0o17; // 15
2. String Literals
let a = 'Hello';
let b = "World";
let c = `Hello World`;
Template Literals (Modern Way)
let name = "Amit";
console.log(`Hello ${name}`);
Features:
Variable embedding (
${})Expressions allowed
Multi-line support
3. Boolean Literals
let isOnline = true;
let isAdmin = false;
Only two values:
truefalse
4. Array Literals
let fruits = ["apple", "banana", "mango"];
let numbers = [1, 2, 3, 4];
let mixed = [1, "hello", true];
Index starts from 0
console.log(fruits[0]); // apple
5. Object Literals
let student = {
name: "Rahul",
age: 21,
city: "Delhi"
};
Access::
console.log(student.name);
6. Special Literals
let a = null; // intentional empty value
let b = undefined; // value not assigned
| Type | Meaning |
|---|---|
| null | intentionally empty |
| undefined | not assigned yet |
Quick Summary




