Skip to content

Latest commit

 

History

History

ghost_array

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Ghost Array

Take a look at this code:

Snippet 1

var arr = [];
arr[999] = 'john';
console.log(arr.length);

What is the result of execute "Snippet 1" code?

1000
__match_answer_and_solution__

Snippet 2

var arr = [];
arr[4294967295] = 'james';
console.log(arr.length);

What is the result of execute "Snippet 2" code?

0
__match_answer_and_solution__


Why?

Because 4294967295 overflows the max number of elements that could be handled by Javascript in Arrays.
__match_answer_and_solution__

Snippet 3

var arr = [];
arr[4294967295] = 'james';
console.log(arr[4294967295]);

What is the result of execute "Snippet 3" code?

"james"
__match_answer_and_solution__


Why?

Javascript arrays can work as objects, dictionaries, when you are using as key any value that can not be handled by Array objects.
__match_answer_and_solution__

Snippet 4

var arr = [];
arr[Number.MIN_VALUE] =  'mary';
console.log(arr.length);

What is the result of execute "Snippet 4" code?

0
__match_answer_and_solution__


Why?

Javascript arrays can work as objects, dictionaries, when you are using as key any value that can not be handled by Array objects.
__match_answer_and_solution__

Snippet 5

var arr = [];
arr[Number.MIN_VALUE] =  'mary';
console.log(arr[Number.MIN_VALUE]);

What is the result of execute "Snippet 5" code?

"mary"
__match_answer_and_solution__


Why?

Javascript arrays can work as objects, dictionaries, when you are using as key any value that can not be handled by Array objects.
__match_answer_and_solution__