Set.prototype.add() — JS Technical Interview Questions & Answers
Adds a value to the Set, ignoring duplicates.
Set.prototype.add()
- Set.prototype.add() 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.add().
- From the example next to this theory: Adds a value to the Set, ignoring duplicates.
- Trap: arrow function for a method that needs this. Fix that before you talk about advanced JS.
- Viva: what is Set.prototype.add()? Then show class Student { constructor(name) {} }. Then name the trap.
Technical Interview Tip: What is Set.prototype.add()? Show this: class Student { constructor(name) {} }. Trap: arrow function for a method that needs this.
Code Examples for Set.prototype.add()
Set.prototype.add() — working example
const tags = new Set(["js", "js", "sql"]);
tags.add("css");
console.log(tags.has("js"), tags.size);
console.log([...tags]);Set.prototype.add() — set stores unique values — great for dedupe and membership checks.
How to Explain Set.prototype.add() to an Interviewer
What is Set.prototype.add()?
Used for class Student a tiny OOP file Not just the heading
Give one small example of Set.prototype.add().
class Student { constructor(name) {} } Adds a value to the Set, ignoring duplicates
What mistake do beginners make with Set.prototype.add()?
arrow function for a method that needs this Show the fix
Where do you use Set.prototype.add()?
a tiny OOP file class Student