Lapys
Lapys

Reputation: 946

How do arrays store empty values - JavaScript

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:

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?

Upvotes: 3

Views: 822

Answers (1)

Our_Benefactors
Our_Benefactors

Reputation: 3539

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.

Upvotes: 2

Related Questions