JavaScript Cheatsheet
Comprehensive reference for JavaScript fundamentals, ES6+, arrays, objects, and async operations.
| Keyword | Description | Syntax | Example | Category |
|---|---|---|---|---|
| const | Declare block-scoped constant variable | const name = value | const PI = 3.14159 | Variables |
| let | Declare block-scoped variable | let name = value | let count = 0 | Variables |
| var | Declare function-scoped variable (legacy) | var name = value | var x = 10 | Variables |
| function | Declare a function | function name(params) { ... } | function add(a, b) { return a + b; } | Functions |
| Arrow Function | ES6 concise function syntax | const name = (params) => { ... } | const add = (a, b) => a + b | Functions |
| Default Parameters | Set default function parameters | function name(param = default) { ... } | function greet(name = "Guest") { ... } | Functions |
| Rest Parameters | Capture remaining function arguments | function name(...args) { ... } | function sum(...nums) { return nums.reduce((a,b) => a+b) } | Functions |
| Spread Operator | Expand iterable into individual elements | ...iterable | const arr = [...[1,2,3], 4] | ES6+ |
| Destructuring | Extract values from objects/arrays | const { x, y } = obj | const { name, age } = user | ES6+ |
| Template Literals | Create strings with expressions | `string ${expression}` | `Hello, ${name}!` | ES6+ |
| map() | Transform array elements | array.map(callback) | [1,2,3].map(x => x * 2) | Arrays |
| filter() | Filter array elements | array.filter(callback) | [1,2,3].filter(x => x > 1) | Arrays |
| reduce() | Reduce array to single value | array.reduce(callback, initial) | [1,2,3].reduce((sum, x) => sum + x, 0) | Arrays |
| forEach() | Iterate over array elements | array.forEach(callback) | [1,2,3].forEach(x => console.log(x)) | Arrays |
| find() | Find first element matching condition | array.find(callback) | [1,2,3].find(x => x > 2) | Arrays |
| Object.keys() | Get object property names | Object.keys(obj) | Object.keys({a: 1, b: 2}) | Objects |
| Object.values() | Get object property values | Object.values(obj) | Object.values({a: 1, b: 2}) | Objects |
| Object.entries() | Get key-value pairs | Object.entries(obj) | Object.entries({a: 1, b: 2}) | Objects |
| Promise | Handle asynchronous operations | new Promise((resolve, reject) => {}) | new Promise(r => setTimeout(() => r("done"), 1000)) | Async |
| async/await | Simplified promise handling | async function() { await promise } | const data = await fetch("/api") | Async |
| try/catch | Error handling in async code | try { ... } catch(e) { ... } | try { await fetch() } catch(e) { console.error(e) } | Async |