C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Set forEach() methodThe JavaScript Set forEach() method executes the specified function once for each value in the Set object. It maintains an insertion order. Unlike Map, the Set object doesn't use key externally. To keep the values unique it uses the key internally. Thus, Set considers the value of element as a key. SyntaxThe forEach() method is represented by the following syntax: setObj.forEach(function callback(value1, value2, Set) { //your iterator }[, thisArg]) Parametercallback - It represents the function to execute. value1, value2 - The value1 represents the key of the element whereas value2 represents the value. Both arguments contains the same value. set - It represents the current Set object. thisArg - It represents the value that treats as this when executing callback. JavaScript Set forEach() method exampleLet's see a simple example of forEach() method <script> var set = new Set(["jQuery","AngularJS","Bootstrap"]); document.writeln("Fetching values :"+"<br>"); set.forEach(function display(value1,value2,set) { document.writeln('key[' + value1 + '] = ' + value2+"<br>"); }) </script> Output: Fetching values : key[jQuery] = jQuery key[AngularJS] = AngularJS key[Bootstrap] = Bootstrap
Next TopicJavaScript Set has() Method
|