I tried running this code. var arrayone = ["Rose","Milk","ghee"] arrayone = arrayone.push("kill") console.log(arrayone) console output is 4 Can anyone explain this.
You don't have to make it arrayone= arrayone.push("kill") because push returns the length of the array so the arrayone variable will be now the length, however if you just call it on the array like arrayone.push then it will add the item and the output will be the new array with the new item
Because it returns length of the array
Using push you added "kill" to the end of "arrayone" array. Now the length of that array is 4 not 3!!! In the end, You changed variable arrayone from array to integer Then you called that var in console
Ruba Kh If, push function returns the value of length in general, then while running console.log(arrayone.push) it should return number?
Why is this returning the length of array?
This is why I recommend reading documentations.
Yup exactly Lucifer It will add an item also try this to understand var arrayone = ["Rose","Milk","ghee"] console.log(arrayone.push("kill")) console.log(arrayone)
@RDC I tried reading the MDN documentation but couldn't find the answer, if you have read it and found the answer, it would be better to if you just can answer.