Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log on a WeakSet give <items unknown> in node.js?

Why does console.log display WeakSet as “<items unknown>“?

[13:37:11] [~] node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let student1 = { name: 'James', age: 26 };
undefined
> let student2 = { name: 'Julia', age: 27 };
undefined
> const roster = new WeakSet([student1, student2]);
undefined
> console.log(roster);
WeakSet { <items unknown> }
undefined

Context: I came across below example on WeakSet in ES6.

let student1 = { name: 'James', age: 26 };
let student2 = { name: 'Julia', age: 27 };
const roster = new WeakSet([student1, student2]);
console.log(roster);

The example suggests it should print

WeakSet {Object {name: 'Julia', age: 27}, Object {name: 'Richard', age: 31}}

But in node v14.4.0 it prints

WeakSet { <items unknown> }
like image 851
kgf3JfUtW Avatar asked May 21 '26 08:05

kgf3JfUtW


1 Answers

The node-js team decided that its hard to correctly implement that. Here is the issue: https://github.com/nodejs/node/issues/19001

So that mean that WeakSet work correctly BUT console.log will always output an empty WeakSet

If you still wanna inspect the WeakMap you can do that by using utils inspect:

const { inspect } = require('util');
let student1 = { name: 'James', age: 26 };
let student2 = { name: 'Julia', age: 27 };
const weakSet = new WeakSet([student1, student2]);
console.log(inspect(weakSet, { showHidden: true }));
like image 114
Hexception Avatar answered May 23 '26 20:05

Hexception



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!