If I do
var a = [].push(undefined);
console.log(a);
it gives output as 1
even though undefined was pushed to the array. Any idea why?
If I do
var a = [].push(undefined);
console.log(a);
it gives output as 1
even though undefined was pushed to the array. Any idea why?
You're testing it the wrong way. a
is not defined as the array, as I think you suppose it to be. Try this:
var a = []
a.push(undefined);
console.log(a);
You are assigning the return value of push
function to variable a
. push
returns the length of the array after pushing the current element in context. So, it returns 1 after pushing undefined
in the array.
its pushing the length of array inside not the elements
example
var a = [].push(5,6,7,8);
console.log(a); //gives 4
There was no explicit check has been done when assigning a new property to array object. Assigning a new property in the sense, setting 0,1,2..n properties with values, based on the length of the array.
- Repeat, while items is not empty
- Remove the first element from items and let E be the value of the element.
- Let setStatus be Set(O, ToString(len), E, true).
- ReturnIfAbrupt(setStatus).
- Let len be len+1.
You can see it here. Step 8.