103/119

Size

JS Functions
Easy
10 min
types
objects
lodash

Implement size(collection). It returns how many items a collection holds. What counts as an "item" depends on the type.

size([1, 2, 3]); // 3
size({ a: 1, b: 2 }); // 2
size('pebbles'); // 7
size('😀ok'); // 3   (code points, not UTF-16 units)
size(new Map([['a', 1]])); // 1
size(new Set([1, 2, 2])); // 2
size(null); // 0
size(42); // 0

Rules

ValueSize
null, undefined0
stringsthe number of Unicode code points ('😀'.length is 2, but its size is 1)
arrays, typed arrays, arguments objectslength
Map, Setsize
any other object, including functions and Object.create(null) objectsthe number of own enumerable string keys
numbers, booleans, symbols, bigints0

Follow-up

  • '👍🏽' (thumbs up with a skin tone) has a size of 2 under these rules. What would a user call its length, and how would you count that?
  • Why can't you use .length on a Map?

Spotted a mistake or a rights concern? Report an issue