Variables
- let variableName = value; //Mutable
- const constantName = value; //Immutable
Data Types
- String: “Hello”
- Number: 50
- Boolean: true or false
- Array: let arr = [1,2,3];
- Object: let obj = {key: ‘value’ };
Functions
- String: “Hello”
- Number: 50
- Boolean: true or false
- Array: let arr = [1,2,3];
- Object: let obj = {key: ‘value’ };
Conditionals
if (condition) {
//Code
} else if (anotherCondition) {
//Code
} else {
//Code
}
Loops
for (let i = 0; i < 5; i++) {
// Code
}
// For…in loop for objects
for (let key in object) {
// Code
}
// For…of loop for arrays
for (let element of array) {
// Code
}
Arrays
- let array = [1, 2, 3];
- array.push(4); // Add to end
- array.pop(); // Remove from end
- array.shift(); // Remove from beginning
- array.unshift(0); // Add to beginning
Objects
let person = {
name: ‘Harris’,
age: 20,
isStudent: true
};
DOM Manipulation
- document.getElementById(‘elementId’);
- document.querySelector(‘.className’);
- element.addEventListener(‘click’, () => {
// Code
});
AJAX and Fetch
- fetch(‘https://api.example.com/data’)
- .then(response => response.json())
- .then(data => console.log(data))
- .catch(error => console.error(‘Error:’, error));
ES6+ Features
- Destructuring:
const { key } = object;
- Spread Operator:
const newArray = [...oldArray];
- Template Literals:
`Hello, ${name}!`
- Promise:
new Promise((resolve, reject) => { /* Code */ });
- Async/Await:
async function fetchData() { /* Code */ }