JavaScript Notes
A comprehensive JavaScript cheat sheet covering variables, data types, operators, strings, arrays, objects, functions, control flow, DOM, events, async programming, ES6+ features, error handling, and useful patterns. Perfect quick reference for beginners and experienced developers.
This is a comprehensive JavaScript quick-reference. Each section contains concise code snippets with their output — a perfect guide for revision and interviews.
2. Data Types
JavaScript has 8 data types — 7 primitives + 1 non-primitive:
// Primitives
let str = "Hello"; // String
let num = 42; // Number
let big = 9007199254740993n; // BigInt
let bool = true; // Boolean
let undef = undefined; // Undefined
let nul = null; // Null
let sym = Symbol("id"); // Symbol
// Non-primitive
let obj = { key: "value" }; // Object (arrays, functions bhi objects hain)
// typeof operator
console.log(typeof str); // "string"
console.log(typeof num); // "number"
console.log(typeof bool); // "boolean"
console.log(typeof undef); // "undefined"
console.log(typeof nul); // "object" (JS bug — legacy reason)
console.log(typeof sym); // "symbol"
console.log(typeof obj); // "object"
console.log(typeof big); // "bigint"string number boolean undefined object symbol object bigint
// Type Conversion
console.log(Number("42")); // 42
console.log(String(123)); // "123"
console.log(Boolean(0)); // false
console.log(Boolean("hello")); // true
console.log(parseInt("3.14")); // 3
console.log(parseFloat("3.14")); // 3.1442 123 false true 3 3.14
3. Operators
Arithmetic
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 3); // 3.333...
console.log(10 % 3); // 1 (remainder)
console.log(2 ** 3); // 8 (exponent)13 7 30 3.3333333333333335 1 8
Comparison
console.log(5 == "5"); // true (loose — type coercion)
console.log(5 === "5"); // false (strict — no coercion)
console.log(5 != "5"); // false
console.log(5 !== "5"); // true
console.log(10 > 5); // true
console.log(10 <= 10); // truetrue false false true true true
Logical
false true false null Guest
Ternary & Assignment
let score = 85;
let grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade);
// Assignment operators
let x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6
x **= 2; // x = 36
console.log(x);B 36
4. Strings — Key Methods
18 HELLO, JAVASCRIPT! hello, javascript! 7 true JavaScript Hi, JavaScript! ["Hello", "JavaScript!"] Hello, JavaScript! true true Hello, JavaScript!Hello, JavaScript! !
Template Literals
let name = "Rahul";
let age = 22;
let msg = `My name is ${name} and I am ${age} years old.`;
console.log(msg);
// Multi-line
let html = `
<div>
<h1>${name}</h1>
</div>`;
console.log(html.trim());My name is Rahul and I am 22 years old. <div> <h1>Rahul</h1> </div>
5. Arrays — Key Methods
[1, 2, 99, 4, 5]
Iteration Methods
[2, 4, 6, 8, 10] [2, 4] 15 4 3 true true 1 2 3 4 5
More Array Methods
[1, 1, 3, 4, 5, 9] [9, 5, 4, 3, 1, 1] true [9, 5, 4, 3, 1, 1] [1, 2, 3] ['h', 'e', 'l', 'l', 'o'] [1, 2, 3]
6. Objects
Amit 28 Hi, I'm Amit ["name", "age", "greet"] ["Amit", 29, ƒ] [["name", "Amit"], ["age", 29], ["greet", ƒ]]
Object Methods
// Object.assign — shallow copy / merge
let a = { x: 1 };
let b = { y: 2 };
let merged = Object.assign({}, a, b);
console.log(merged); // { x: 1, y: 2 }
// Spread merge (preferred)
let merged2 = { ...a, ...b, z: 3 };
console.log(merged2); // { x: 1, y: 2, z: 3 }
// Object.freeze — immutable
let config = Object.freeze({ api: "https://api.example.com" });
config.api = "changed"; // silently fails
console.log(config.api);
// Check property
console.log("x" in a); // true
console.log(a.hasOwnProperty("x")); // true{ x: 1, y: 2 }
{ x: 1, y: 2, z: 3 }
https://api.example.com
true
true7. Functions
Function Declaration & Expression
// Declaration (hoisted)
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // 7
// Expression (NOT hoisted)
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(3, 4)); // 12
// Default parameters
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
console.log(greet()); // "Hello, Guest!"
console.log(greet("Priya")); // "Hello, Priya!"7 12 Hello, Guest! Hello, Priya!
Arrow Functions
25 30 120 JS undefined
IIFE (Immediately Invoked Function Expression)
IIFE executed: hidden Arrow IIFE 42
8. Control Flow
if / else if / else
let temp = 35;
if (temp > 40) {
console.log("It is very hot! 🥵");
} else if (temp > 30) {
console.log("Garmi hai ☀️");
} else if (temp > 20) {
console.log("Suhana mausam 🌤️");
} else {
console.log("Thanda hai ❄️");
}Garmi hai ☀️
for Loops
0 1 2 H i brand: Tesla year: 2026
while & do...while
0 1 2 j = 10
switch
Working day 💼
9. DOM Manipulation
Selectors
Create, Modify & Remove
// Create element
const div = document.createElement("div");
div.textContent = "Hello World";
div.className = "card";
div.id = "myCard";
div.setAttribute("data-id", "123");
div.style.color = "blue";
div.style.padding = "1rem";
// Append to DOM
document.body.appendChild(div);
// Insert before
const parent = document.getElementById("list");
const newItem = document.createElement("li");
newItem.textContent = "New Item";
parent.insertBefore(newItem, parent.firstChild);
// Remove
const old = document.getElementById("old");
old.remove(); // modern
// old.parentNode.removeChild(old); // legacy
// innerHTML vs textContent
el.innerHTML = "<strong>Bold</strong>"; // parses HTML
el.textContent = "<strong>Bold</strong>"; // plain text (safer)Class & Attribute Manipulation
const box = document.querySelector(".box");
// Classes
box.classList.add("active", "visible");
box.classList.remove("hidden");
box.classList.toggle("dark-mode");
console.log(box.classList.contains("active")); // true
// Attributes
box.setAttribute("role", "button");
console.log(box.getAttribute("role")); // "button"
box.removeAttribute("role");
console.log(box.dataset.userId); // reads data-user-id attributetrue button
10. Events
Common Events
11. Async JavaScript
Callbacks
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Cheat Sheet 2026 — Complete Quick Reference.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, notes, cheat, sheet
Related JavaScript Master Course Topics