ES6 Collections 🎯

beginner
17 min

ES6 Collections 🎯

Welcome to our deep dive into ES6 Collections! In this tutorial, we'll explore the game-changing additions to JavaScript, focusing on Arrays, Sets, and Maps. By the end, you'll have a strong foundation to tackle real-world projects with confidence.

Arrays 📝

Arrays are a fundamental part of JavaScript, but ES6 introduces some powerful new features.

Array Methods 💡

  • forEach(): Loops through each element in an array, without returning a value.
  • map(): Creates a new array with the results of calling a provided function on every element in the original array.
  • filter(): Returns a new array with all elements that pass a test provided by the function.
  • reduce(): Reduces the array to a single value by repeatedly applying a function to each element.

Array Destructuring 💡

Simplify your code by extracting values from arrays into variables.

javascript
const [first, second] = [1, 2, 3]; console.log(first); // 1 console.log(second); // 2

Array Spread Operator 💡

Spread an array's contents into another array, or as individual arguments in a function call.

javascript
const numbers = [1, 2, 3]; const newNumbers = [...numbers, 4]; console.log(newNumbers); // [1, 2, 3, 4]

Sets 💡

Sets are collections of unique values, where order is not significant.

Creating Sets 💡

javascript
const mySet = new Set([1, 2, 2, 3, 3, 4]); console.log(mySet); // Set {1, 2, 3, 4}

Set Methods 💡

  • add(): Adds a new element to the set.
  • delete(): Removes an element from the set.
  • has(): Checks if an element exists in the set.
  • clear(): Removes all elements from the set.

Maps 💡

Maps are key-value pairs, where keys can be any data type.

Creating Maps 💡

javascript
const myMap = new Map([ ['key1', 'value1'], ['key2', 'value2'], ]); console.log(myMap); // Map { 'key1' => 'value1', 'key2' => 'value2' }

Map Methods 💡

  • set(): Sets or updates a value associated with a key.
  • get(): Retrieves a value by its key.
  • delete(): Removes a key-value pair from the map.
  • has(): Checks if a key exists in the map.
  • clear(): Removes all key-value pairs from the map.

Quiz 📝

Quick Quiz
Question 1 of 1

What is the output of the following code?

Keep learning, keep coding! 💡