7

According to ECMAScript there are null and undefined datatypes apart from others. But recently I got to see something called empty. Let's regenerate it first (I'll go from basic):

let a = new Array() //Returns [] length=0

let b = new Array(3) //Returns [empty*3] length=3

let c = new Array(3).fill() //Return [undefined,undefined,undefined] length=3

Now, what? See all the elements defined are undefined, which they should be

  • a[0]=undefined
  • b[0]=undefined
  • c[0]=undefined

Main thing: If suppose add an element into c at position 5 i.e a[5]=4 then c will be [undefined,undefined,undefined,empty,4]

What is this empty thing in between? And if everything is same what is the difference in starting initialization of b and c ( I'm talking about [empty*3] & [undefined,undefined,undefined]).

The Answer suggested [here]: What's the difference between empty items in a JavaScrip array and undefined? Doesn't explains the properly about what exactly it is. Yes it can't be undefined as undefined is a value but what about null and if not null why empty is not a data-type?

  • Node.js is largely just V8 which makes it very similar to Chrome in terms of pure JavaScript. – tadman Aug 20 '18 at 17:46
  • Yes, Node.js behaves in a similar fashion. The browser console I'm talking about is spidermonkey. Anyway results are same. –  Aug 20 '18 at 17:48
  • 2
    `empty` is not actually a "thing" in JavaScript. The reason your console spits out `empty` is because that it its attempt to clearly represent a [sparse array](http://2ality.com/2012/06/dense-arrays.html). JS consoles are not bound by any standard, so different consoles may represent these holes in the array differently – mhodges Aug 20 '18 at 20:22
  • For example, the [repl.it](https://repl.it/@ugam44/IndelibleSorrowfulVariety) console returns `[ undefined, undefined, undefined, <1 empty item>, 4 ]` – mhodges Aug 20 '18 at 20:25

2 Answers2

12

Arrays are objects. That means that your array

  [undefined,undefined,undefined,empty,4]

could be written as such an object:

 {
  0: undefined,
  1: undefined,
  2: undefined,
  // 3 doesnt exist at all
  4: 4,
  length: 5,
 }

So undefined is actually a slot that exists and holds a value while 3 is not even a key-value pair. As accessing a non existing key results in undefined there is no real world difference:

    array[2] // undefined
    array[3] // undefined

But iterating over an array with map and others will skip empty ones, so then you should use .fill before iterating.

The only way to detect a difference is to check if the key exists:

  2 in array // true
  3 in array // false

To turn empty to undefined you could set the key:

  array[3] = undefined;

and to turn undefined into empty you have to remove the key:

  delete array[3]

however that rarely matters.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

empty is not a type. It is just a way of describing that there is nothing there. It isn't undefined or null. It is the complete lack of a value. You'll only see that when it tries to show you a representation of the array. If you actually look in those indices, it will return undefined.

dontangg
  • 4,729
  • 1
  • 26
  • 38