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