What is "undefined x 1" in JavaScript?

12,542

Solution 1

That seems to be Chrome's new way of displaying uninitialized indexes in arrays (and array-like objects):

> Array(100)
[undefined × 100]

Which is certainly better than printing [undefined, undefined, undefined,...] or however it was before.

Although, if there is only one undefined value, they could drop the x 1.

Solution 2

Newer versions of Chrome use empty instead of undefined to make the difference a bit clearer.

For example, entering [,,undefined,,] in the DevTools console results in:

▼(4) [empty × 2, undefined, empty]
   2: undefined
   length: 4
  ▶__proto__: Array(0)

Solution 3

Google Chrome seems to choose to display sparse array using this undefined x n notation. It will show [undefined, undefined] if it is not a sparse array:

var arr = new Array(2);
console.log(arr);

var arr2 = [];
arr2[3] = 123;
console.log(arr2);

var arr3 = [,,,];
console.log(arr3)

var arr4 = [,];
console.log(arr4)

var arr5 = [undefined, undefined]
console.log(arr5)

var arr6 = [undefined]
console.log(arr6)

arr1 to arr4 are all sparse arrays, while arr5 and arr6 are not. Chrome will show them as:

[undefined × 2] 
[undefined × 3, 123] 
[undefined × 3] 
[undefined × 1] 
[undefined, undefined] 
[undefined] 

note the [undefined x 1] for the sparse array.

Since you deleted an element, it follows that: If you delete an element from an array, the array becomes sparse.

Solution 4

Here is how it looks, Type this code into chrome devtools console:-

arr = new Array(4);
arr[2] = "adf";
console.log(arr);    // [undefined × 2, "adf", undefined × 1]

See the first two consecutive "undefined" that are represented by *2, to show that there are two consecutive undefined there

Share:
12,542
benqus
Author by

benqus

Full-stack JavaScript Engineer with 10 years of experience Technologies: React, lit-element, webcomponents, TypeScript, WebSocket, WebRTC, NodeJS, ExpressJS, Knex, Objection, GraphQL, RabbitMQ, ElasticSearch, PostgreSQL, Jest, Karma, Mocha, Docker, AWS, Heroku, Git, Cordova/PhoneGap Web and UI design background - Adobe Photoshop, Illustrator, XD, Creative Cloud, Maxon Cinema4D, Autodesk Maya Fan of simple and effective solutions to avoid over-designing and over-spending Worked in FinTech, Media and live SportsTech businesses Love agile and lean methodologies Previous hiring, mentoring and team building experience Prefer working in an open, transparent and constructive community Avid Mountain biker, rower, lifter, hobby mechanic and eater of many foods.

Updated on June 14, 2022

Comments

  • benqus
    benqus about 2 years

    I'm doing some small experiments based on this blog entry.

    I am doing this research in Google Chrome's debugger and here comes the hard part.

    What the heck is this?!

    I get the fact that I can't delete local variables (since they are not object attributes). I get that I can 'read out' all of the parameters passed to a function from the array called 'arguments'. I even get it that I can't delete and array's element, only achieve to have array[0] have a value of undefined.

    Can somebody explain to me what undefined x 1 means on the embedded image?

    And when I overwrite the function foo to return the arguments[0], then I get the usual and 'normal' undefined.

    This is only an experiment, but seems interresting, does anybody know what undefined x 1 refers to?