JavaScript_Cheatsheet
JavaScript_Cheatsheet
- Declaring Variables:
let x = 5; // Block-scoped variable
const y = 10; // Constant value (cannot be reassigned)
var z = 20; // Function-scoped variable (legacy)
- Data Types:
- String: 'Hello', "World"
- Number: 42, 3.14
- Boolean: true, false
- Object: { key: "value", age: 25 }
- Array: [1, 2, 3, 4]
- Null: null
- Undefined: undefined
- Symbol: Symbol('unique')
2. Operators
- Arithmetic Operators:
+, -, *, /, %, ++, --
- Comparison Operators:
==, === (strict), !=, !== (strict), >, <, >=, <=
- Logical Operators:
&& (AND), || (OR), ! (NOT)
- Assignment Operators:
=, +=, -=, *=, /=, %=, **=
- Ternary Operator:
condition ? expr1 : expr2;
3. Conditional Statements
- If-Else:
if (condition) {
// Code to execute
} else {
// Code to execute
}
- Switch Statement:
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute
}
4. Loops
- For Loop:
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}
- While Loop:
let i = 0;
while (i < 5) {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
}
- Do-While Loop:
let i = 0;
do {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
} while (i < 5);
5. Functions
- Function Declaration:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
- Function Expression:
const add = function(x, y) {
return x + y;
};
console.log(add(2, 3));
- Arrow Functions:
const multiply = (x, y) => x * y;
console.log(multiply(3, 4));
6. Arrays
- Creating an Array:
let fruits = ["Apple", "Banana", "Cherry"];
- Array Methods:
fruits.push("Grapes"); // Adds element to the end
fruits.pop(); // Removes last element
fruits.shift(); // Removes first element
fruits.unshift("Orange"); // Adds element to the beginning
7. Objects
- Creating an Object:
let person = {
name: "John",
age: 30,
greet: function() {
return "Hello!";
}
};
8. DOM Manipulation
- Select an Element:
const element = document.getElementById("myElement");
const elements = document.querySelectorAll(".myClass");
- Modify Content:
element.innerHTML = "New content!";
9. Error Handling
- Try-Catch Block:
try {
let result = riskyOperation();
} catch (error) {
console.error("An error occurred:", error);
}
- Template Literals:
let name = "Alice";
let greeting = `Hello, ${name}!`;
- Destructuring Assignment:
const person = { name: "John", age: 30 };
const { name, age } = person;
- Spread Operator:
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
- Rest Parameters:
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
- Promises:
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Success!");
} else {
reject("Failure");
}
});
- Async/Await:
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 fetching data:", error);
}
}
fetchData();
13. Miscellaneous
- Math Methods:
Math.random(); // Random number between 0 and 1
Math.floor(3.14); // 3
Math.ceil(3.14); // 4
- Date Methods:
let date = new Date();
console.log(date.getFullYear());