ES6 Data Structures

Nikhil M

1 min read

From the archive: this article was published in July 2016 and is kept for reference. Tools, versions and figures may have changed since.

On this page 3

ES6 introduces many new features to embrace the developer as well as the browsers to perform in a optimal performant way.

Set

Set is new type of data structure that we can use to store Unique values which can be of any type but they must all be unique.

Create a Set

To create a set we use Set constructor var mySet = new Set();

Add Values to set

let myArray = [1, 4, 1, 5, 5, 7, 8, 9, 0, 0]
let mySet = new Set(myArray)

Now if you do a console.log(mySet); you will see Set {1, 4, 5, 7, 8, 9, 0}

Map

Map is similar to Set but it manges key-value pair instead of individual values.

Create a Map

var myMap = new Map(); will create an instance of Map and to add values to it we can do let myOtherMap = new Map([['a', 2], [1, 'b']])

console.log(myOtherMap); will show Map {"a" => 2, 1 => "b"}

WeakSet and WeakMap

Similar to regular Map and Set but the difference is that the object references inside them are held weakly. So it wont prevent garbage collection.

This article originally appeared on lightrains.com

Leave a comment

To make a comment, please send an e-mail using the button below. Your e-mail address won't be shared and will be deleted from our records after the comment is published. If you don't want your real name to be credited alongside your comment, please specify the name you would like to use. If you would like your name to link to a specific URL, please share that as well. Thank you.

Comment via email

Share this article

Topic

More on Engineering Insights

Tools, languages and practices from the Lightrains team, and how offshore engineering teams work.

Written by

Nikhil M

Managing Director

Entrepreneur / Privacy Freak / Humanist / Blockchain / AI / Digital Security / Online Privacy

Keep reading

Ready to build your next AI product?

Get a free consultation and project quote for AI, software or product development, tailored to your goals.

  • No-obligation consultation
  • Clear scope and timeline
  • Transparent pricing
Get your free project quote