3

Not too long ago, I discovered that arrays in JavaScript need not contain an ordered set of keys (0-x) to store values within it

and some numeric keys may not be defined (0-4 ... 6-x, where 5 is not defined).


And this creates semantically two types of arrays that are similar:

arrayA = [, ,] (partially-empty arrays or sparse arrays)

arrayB = [undefined, undefined] (filled arrays)


But recently, I was tinkering with JavaScript in the Google Chrome Developer Console and came across this:

enter image description here

Now the second array is like arrayA, and the third like arrayB as shown in the console.

But the first array ([...'🏃🏽‍♀️'])... what is it?

I opened up its directory and saw the elements that were defined as hole were undefined with their respective keys in the array.

enter image description here

I also ran a few types of JavaScript loops on the array:

  • for...in statement captures all elements, except the *hole*s.
  • for...of statement captures all elements, except the *hole*s and proceeds to throw an error that the iterator variable used is undefined i.e.:

    for (var value of [...'🏃🏽‍♀️']) console.log(value);

    // Throw 'ReferenceError' when loop is done.

  • Array.prototype.forEach method captures all elements, except the *hole*s.

  • do...while, for and while statements captures all elements, except the *hole*s.

Why does the console see those values as different from empty or undefined (as with arrayA and arrayB)?


The main question is: Is there implicitly another type of array and if so, is there anything to note about it?

8
  • 4
    Possible duplicate of Empty slots in JavaScript objects? Commented Feb 7, 2019 at 1:47
  • 1
    See: ecma-international.org/ecma-262/7.0/#sec-array-exotic-objects Commented Feb 7, 2019 at 1:48
  • 1
    @vsemozhetbyt. Here ya go: [...'🏃🏽‍♀️'] Commented Feb 7, 2019 at 2:10
  • 2
    @Lapys ... splits by codepoints. And that single emoji is comprised of multilple codepoints, see the link I posted above Commented Feb 7, 2019 at 2:11
  • 2
    I cannot understand the holes/undefined's at the end of the spread string. What do they represent in the cluster? I have only 5 codepoints from the code above in the comment, without holes/undefined's in my Chrome Canary console. Commented Feb 7, 2019 at 2:39

1 Answer 1

2

The ... is known as spread syntax. Read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Emojis are made up of a variety of elements which the browser renders as a single emoji. Here's a quick article that expands on that. https://til.hashrocket.com/posts/2f488279a3-expand-emojis-with-the-spread-operator

By applying the spread syntax to an emoji, you can look at the individual emojis it's composed of.

Sign up to request clarification or add additional context in comments.

3 Comments

... is not an operator, even the link you referred to does not call it an "operator".
@zerkms funnily enough, it's in the directory /Operators/.
Never mind, I just found out what's happening at MDN: they put bot expressions and operators under the directory /Operators/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.