JavaScript Skills
Variables & Types
Variables
let name = "JavaScript";
const PI = 3.14159;
var oldStyle = "deprecated";
typeof name;
typeof 42;
typeof true;
Functions
Regular Function
function greet(name) {
return `Hello, ${name}!`;
}
Arrow Function
const greet = (name) => `Hello, ${name}!`;
const multiply = (a, b) => {
return a * b;
};
Callback
const processData = (data, callback) => {
const result = doSomething(data);
callback(result);
};
Arrays
Array Methods
const arr = [1, 2, 3, 4, 5];
arr.map(x => x * 2);
arr.filter(x => x > 2);
arr.reduce((sum, x) => sum + x, 0);
arr.find(x => x === 3);
arr.forEach(x => console.log(x));
Objects & Destructuring
Object
const person = {
name: "John",
age: 30,
greet() {
return `Hello, I'm ${this.name}`;
}
};
person.name;
person['age'];
Destructuring
const { name, age } = person;
const [first, second] = arr;
Async/Await & Promise
Promise
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("data");
}, 1000);
});
};
Async/Await
const getData = async () => {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
};
Promise.all
const [result1, result2] = await Promise.all([
fetch(url1),
fetch(url2)
]);
Common Patterns
Optional Chaining
const value = obj?.nested?.property ?? "default";
Nullish Coalescing
const value = input ?? "default value";
Spread Operator
const merged = { ...obj1, ...obj2 };
const combined = [...arr1, ...arr2];