0% found this document useful (0 votes)
4 views2 pages

Javascript Cheat Sheet

The document outlines essential programming concepts including variables, data types, functions, and control structures. It covers DOM manipulation, event handling, array and object methods, local storage, and advanced JavaScript features like destructuring and the spread/rest operator. Each section provides code examples to illustrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Javascript Cheat Sheet

The document outlines essential programming concepts including variables, data types, functions, and control structures. It covers DOM manipulation, event handling, array and object methods, local storage, and advanced JavaScript features like destructuring and the spread/rest operator. Each section provides code examples to illustrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LEVEL 1: BASICS YOU MUST MASTER

Variables:
let name = "David"; // changeable
const age = 18; // fixed value

Data Types:
- String -> "hello"
- Number -> 21
- Boolean -> true / false
- Array -> ["red", "blue"]
- Object -> { name: "David", age: 18 }

Functions:
function greet(name) {
return "Hello " + name;
}

// Arrow version
const greet = (name) => "Hello " + name;

If/else:
if (age >= 18) {
console.log("You're an adult");
} else {
console.log("Too young");
}

Loops:
for (let i = 0; i < 5; i++) {
console.log(i);
}

let colors = ["red", "blue"];


colors.forEach((color) => console.log(color));

LEVEL 2: DOM & EVENTS


Selecting:
let btn = document.querySelector("#click-me");

Changing content:
btn.textContent = "Clicked!";

Adding events:
btn.addEventListener("click", () => {
alert("Button clicked!");
});

Changing classes/styles:
btn.classList.add("active");
btn.style.color = "red";

LEVEL 3: ARRAYS & OBJECTS


Useful array methods:
let nums = [1, 2, 3, 4];

nums.map(n => n * 2); // [2, 4, 6, 8]


nums.filter(n => n > 2); // [3, 4]
nums.reduce((a, b) => a + b); // 10

Looping through objects:


let user = { name: "David", age: 18 };

for (let key in user) {


console.log(key, user[key]);
}

LEVEL 4: LOCAL STORAGE


localStorage.setItem("theme", "dark");

let theme = localStorage.getItem("theme");

localStorage.removeItem("theme");

LEVEL 5: LOGIC YOU'LL NEED FOR REACT


Destructuring:
let person = { name: "David", age: 18 };
let { name, age } = person;

Spread/rest:
let nums = [1, 2, 3];
let newNums = [...nums, 4]; // [1, 2, 3, 4]

function sum(...numbers) {
return numbers.reduce((a, b) => a + b);
}

You might also like