Hows Set Object is implemented

Just anyone know how the Set object is implemented in JavaScript? When instance of Set with properties is consol logged the output is as follow

const set = new Set([1,2,2,3]);
console.log(set) // -> Set  {1,2,3} 

To make it short the only one thing which interesting me is why there is no .keys() or values() and how to force instance to give similar output;

Hi @ReaZ ,
Amit here.

Set does have .keys() and .values() on it. Though it returns you back Iterable Object, you can use .next() method to check key/values.

Check this for deep dive into Set objects

Map and Set

I hope it helps, Thank you!


Thanks for quick answer.

I just did some tests and indeed, it does return iterable Object, but i would be glad to see implementation how to make instance return iterable Object like Set does.

1 Like

I guess you’re interested in implementation details?
If Yes?

Does JavaScript have an implementation of a set data structure? - Stack Overflow

Hope it helps, Good Night.

1 Like

Well, took me some time to get trought specs and all the ressources, so far found out how to return iterable object from class methods. In shared post of Stack Overflow there was github repo with implementation of Set es6-set/polyfill.js at master · medikoo/es6-set · GitHub but its including functions which can’t be found there, like

 , d              = require('d')

Still looking for a way to make object looks as follow:

just to make it appear like array but with curly brackets {} // Set { values }

I don’t think I’m following what you are asking here? Are you asking how to make it print out a certain way when you do a console.log? The formatting used in a console.log is up to the console. For example, if I console.log a Set in FF 92 on linux it prints out using square brackets but if I do the same thing in node it uses curly braces. If you want to have complete control over this you should write your own method that creates the string you want and then console.log that string.

But I’m not exactly sure this is what you are asking. If not can you be a little more specific?


Well to make it as simple as possible:

Want constructor of CustomSet to return spreaded [Iterator] Object in same way as native Set does. So logged customSet should show

CustomSet { 1, 2, 3 }  instead of CustomSet { [Iterator] }

Why?!?! What it looks like with console.log has absolutely no bearing on how the set works.

Read again the question, I simplyfy it enought, even begginer programmers could catch it. Don’t go qround the topic, there is only one problem to solve, dont look for new ones.

I’ve been reading your posts, thanks.

Again… how the object appears with console.log() has absolutely zero bearing on how it actually functions, so what goal are you actually trying to achieve by changing how this object console.log()s?

Goal to achieve:

Thought its obvious?:

Just curiosity, since couldnt find implementation of native Set and polyfills of it. :slight_smile:

How something is console.log()ed is controlled by the JavaScript implementation and has absolutely nothing to do with how it behaves. You would have to dig into different JS engines to see how each engine decides to console.log() different objects and user defined objects.

You have zero ability to control how something appears when console.log()ed. JS doesn’t have __str__() or __repr__() the way that Python does.

Well i didn’t ask about python but good to know.
Implementations have to follow the specs, so the only difference in given output of Set object is beside curly and square brackets
If anyone know how to make Iterator Object appear in same way as native Set does J would be glad if anyone share the knowledge

Goal to achieve:

I’ve peeked at descriptors of Set, and the only one thing which i couldnt imitiate in same way as is showed below is the marked property. It may be the key :slight_smile:

This is not something you have any control over whatsoever.

I know you don’t want to hear or believe this, but it is true.

Specification Part 2

The logger operation accepts a log level and a list of other arguments. Its main output is the implementation-defined side effect of printing the result to the console. This specification describes how it processes format specifiers while doing so.

Its main output is the implementation-defined side effect of printing the result to the console.


Printer Specification

The printer operation is implementation-defined. It accepts a log level indicating severity, a List of arguments to print, and an optional object of implementation-specific formatting options. Elements appearing in args will be one of the following:

The printer operation is implementation-defined.


If you want to know how the logging happens in a particular JS engine, you need to dig into that engine’s implementation. JS does not offer control over the display of user defined objects in the way that Python does.

And sometimes specs don’t specify every minute detail such as how to display something in console output. These types of decisions are left up to the vendor.


One idea I have for you is to override the toString method on your custom set and then force it to be displayed as a string when you send it to console.log.

1 Like

Yup. You can create a custom method to override toString() and then you can determine the output from console.log(mySuperSpecialSet.toString()) or console.log("" + mySuperSpecialSet), but you have no control over console.log(mySuperSpecialSet).

Its not about beliving, everything is doable just need to find out way how.

Tryed it already, its working but would like to get same behaviour as the native Set.

Just to make it clear, it isn’t question from beginner or newbie, its open discussion. If anyone know how to imitiate native Set behaviour feel free to share;

This is unfortunately not true. The JS language standard very specifically contradicts you here. console.log()'s behavior is implementation specific. No tools in the language specification exist for you to control this.

The fact that you don’t believe the language specification as written is not the fault of the language specification.

1 Like

Since this implementation specific by the JS engine, I suppose technically this is “doable” by writing your own JS engine…

1 Like