Data structures
Summary of fundamental concepts:
| Structure | Key characteristics | Code example | Allowed keys | Preferred use cases | Key differences |
|---|---|---|---|---|---|
| Object | - Stores key-value pairs. - Keys can only be strings or symbols. - Does not maintain insertion order (except for numbers). |
js const obj = { key: "value", 42: "number key" }; console.log(obj.key); |
Strings, Symbols | When keys are known strings in advance and fast access is needed. | Not iterable with for...of. Accessed with obj.key or obj["key"]. |
| Array | - Ordered collection of values. - Accessible by numeric index. - Useful methods like .push(), .map(), .filter(). |
js const arr = [1, 2, 3]; console.log(arr[0]); |
Numeric indices | Ordered lists of elements when order and position matter. | Maintains insertion order. Iterable with for...of. |
| Map | - Key-value pairs, but keys can be of any type. - Maintains insertion order. |
js const map = new Map(); map.set("key", 42); console.log(map.get("key")); |
Any data type (including objects and functions) | When search efficiency is needed and keys can be of any type. | Better performance than Object for large data sets. |
| Set | - Collection of unique values (not key-value pairs). - Does not allow duplicates. - Maintains insertion order. |
js const set = new Set([1, 2, 3, 3]); console.log(set.has(2)); |
No keys, only unique values. | When a list without duplicates is needed or to quickly check if a value is present. | Does not allow index access like an Array. |
Methods .map, .filter, .forEach
| Method | Description | Code example | Return value | Use cases | Key differences |
|---|---|---|---|---|---|
| .map() | Creates a new array by applying a function to each element of the original array. | js const nums = [1, 2, 3]; const doubled = nums.map(n => n * 2); console.log(doubled); // [2, 4, 6] |
New transformed array | When each element needs to be transformed and a new array returned. | Does not modify the original array. Always returns a new array. |
| .filter() | Creates a new array with elements that meet a condition. | js const nums = [1, 2, 3, 4]; const evens = nums.filter(n => n % 2 === 0); console.log(evens); // [2, 4] |
New array with filtered elements | When specific elements need to be extracted from an array. | Does not modify the original array. Returns only elements that meet the condition. |
| .forEach() | Executes a function for each element of the array without returning a new array. | js const nums = [1, 2, 3]; nums.forEach(n => console.log(n * 2)); |
undefined (returns nothing) |
When an action needs to be performed on each element without creating a new array. | Does not return an array, only iterates over elements. Modifies the array if changes are made inside the function. |
Spread and Rest Operator in JavaScript
Spread Operator (...)
The spread operator is used to make copies and combinations of arrays and objects without modifying the originals. Its main function is to expand the elements of an iterable into a new context.
Example: Copy and combine arrays
| Text Only | |
|---|---|
1 2 3 | |
It also works with objects:
| Text Only | |
|---|---|
1 2 3 | |
Rest Operator (...)
The rest operator allows grouping multiple values into a single variable. It is useful in functions when the number of arguments to be received is not known in advance. It is also used in destructuring to split an array or object into parts.
Example in functions
| Text Only | |
|---|---|
1 2 3 4 | |
Example in array destructuring
| Text Only | |
|---|---|
1 2 3 | |
In objects, the rest operator allows separating some properties and grouping the rest into a new object: const { a, ...rest } = { a: 1, b: 2, c: 3 }; console.log(a); // 1 console.log(rest); // { b: 2, c: 3 }