Javascript Set forEach()

Introduction

The Javascript Set forEach() method runs a function on each value in the Set object.

It runs on the elements in insertion order.

mySet.forEach(callback[, thisArg])
  • callback - function to execute for each element:
  • thisArg - Optional, value to use as this when executing callback.

callback function takes three parameters:

  • the element value
  • the element key
  • the Set object being traversed

Logging the contents of a Set object

function logSetElements(value1, value2, set) {
    console.log('s[' + value1 + '] = ' + value2);
}

new Set(['foo', 'bar', undefined]).forEach(logSetElements);



PreviousNext

Related