Set.prototype.forEach() — JS Technical Interview Questions & Answers

Runs a callback for every value, in insertion order.

Set.prototype.forEach()

  • Set.prototype.forEach() is a JS topic. In plain words you use it for class Student in a tiny OOP file. Don’t start with a slogan — start with that picture.
  • Smallest example: class Student { constructor(name) {} }. Type it, run it, and say what you see. If you can do that from memory, you know Set.prototype.forEach().
  • From the example next to this theory: Runs a callback for every value, in insertion order.
  • Trap: arrow function for a method that needs this. Fix that before you talk about advanced JS.
  • Viva: what is Set.prototype.forEach()? Then show class Student { constructor(name) {} }. Then name the trap.

Technical Interview Tip: What is Set.prototype.forEach()? Show this: class Student { constructor(name) {} }. Trap: arrow function for a method that needs this.

Code Examples for Set.prototype.forEach()

Set.prototype.forEach() — working example

const tags = new Set(["js", "js", "sql"]);
tags.add("css");
console.log(tags.has("js"), tags.size);
console.log([...tags]);

Set.prototype.forEach() — set stores unique values — great for dedupe and membership checks.

How to Explain Set.prototype.forEach() to an Interviewer

What is Set.prototype.forEach()?

Used for class Student a tiny OOP file Not just the heading

Give one small example of Set.prototype.forEach().

class Student { constructor(name) {} } Runs a callback for every value, in insertion order

What mistake do beginners make with Set.prototype.forEach()?

arrow function for a method that needs this Show the fix

Where do you use Set.prototype.forEach()?

a tiny OOP file class Student