Why I am getting number as output when I’m using array.push [closed]

Use arr.push('ghi') instead of arr = arr.push('ghi')

let arr = ['abc','def'];
arr.push('ghi')
console.log(arr);

According to MDN

The push() method adds one or more elements to the end of an array and
returns the new length of the array.

So when you store the return value of arr.push() function into arr, this returns you the length of the array after pushing the new value. So the value of arr becomes 3.

Leave a Comment