Undefined Values in Arrays

See the next snippet of code:

var arr = [];
arr.length = 10;
// The next statement shows [] in the console.
console.log(arr);
// The next statement shows [undefined × 10] in the console.
arr;
// The next statement shows undefined in the console.
console.log(JSON.stringify(arr[0]));

Snippet 1

console.log(JSON.stringify(arr));

What will be shown in the console if we execute Snippet 1?

[null,null,null,null,null,null,null,null,null,null]
__match_answer_and_solution__

Why?

When JSON.stringify is called it call toJSON method of object but undefined is not an object itself then it calls the object that is in the backend of undefined, returning 'null' as value, this is the reason of undefined == null is true.
__match_answer_and_solution__

Snippet 2

console.log(arr.toString());

What will be shown in the console if we execute Snippet 2?

",,,,,,,,,"
__match_answer_and_solution__

Why?

When the toString of arr is called it returns an empty string for each undefined value in the array.
__match_answer_and_solution__

Last updated

Was this helpful?