Here’s a handy JavaScript cheat sheet with some common concepts and syntax.
let x = 10; // Block-scoped variable
const y = 20; // Block-scoped, constant variable
var z = 30; // Function-scoped variable
string, number, boolean, null, undefined, symbol, bigintlet name = "John"; // string
let age = 25; // number
let isActive = true; // boolean
let unknown = null; // null
let notDefined; // undefined
+, -, *, /, %, ++, --==, ===, !=, !==, >, <, >=, <=&&, ||, !=, +=, -=, *=, /=function sum(a, b) {
return a + b;
}
const multiply = function(a, b) {
return a * b;
};
const divide = (a, b) => a / b;
if (condition) {
// code to execute if condition is true
} else if (anotherCondition) {
// code to execute if anotherCondition is true
} else {
// code to execute if all conditions are false
}
switch (expression) {
case value1:
// code to execute if expression === value1
break;
case value2:
// code to execute if expression === value2
break;
default:
// code to execute if no cases match
}
for (let i = 0; i < 5; i++) {
// code to execute in each iteration
}
let i = 0;
while (i < 5) {
// code to execute as long as condition is true
i++;
}
let i = 0;
do {
// code to execute at least once and then continue if condition is true
i++;
} while (i < 5);
let numbers = [1, 2, 3, 4, 5];
push(): Adds elements to the end.pop(): Removes the last element.shift(): Removes the first element.unshift(): Adds elements to the beginning.slice(): Returns a portion of an array.splice(): Adds/removes elements at a specific index.forEach(): Calls a function for each array element.map(): Creates a new array with the results of calling a function on every element.filter(): Creates a new array with all elements that pass the test implemented by the provided function.let person = {
firstName: "John",
lastName: "Doe",
age: 30,
greet: function() {
console.log("Hello!");
}
};
console.log(person.firstName); // Dot notation
console.log(person['lastName']); // Bracket notation
Object.keys(obj): Returns an array of the object’s keys.Object.values(obj): Returns an array of the object’s values.Object.entries(obj): Returns an array of the object’s key-value pairs.const element = document.getElementById("id");
const elements = document.getElementsByClassName("class");
const elements = document.querySelectorAll("selector");
element.textContent = "New Text";
element.innerHTML = "<strong>Bold Text</strong>";
element.style.color = "blue";
element.addEventListener("click", function() {
// code to execute on click
});
click: User clicks an element.mouseover: User hovers over an element.keydown: User presses a key.let promise = new Promise(function(resolve, reject) {
// asynchronous code
if (success) {
resolve(result);
} else {
reject(error);
}
});
promise
.then(function(result) {
// handle success
})
.catch(function(error) {
// handle error
});
fetch("https://api.example.com/data")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
async function fetchData() {
try {
let response = await fetch("https://api.example.com/data");
let data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
try {
// code that may throw an error
} catch (error) {
console.error(error);
} finally {
// code that runs regardless of an error
}
let greeting = `Hello, ${name}!`;
let {firstName, lastName} = person;
let [first, second] = numbers;
let newArray = [...numbers, 6, 7];
let newObject = {...person, city: "New York"};