Arrays : adding items

const cities = ["Manchester", "Liverpool"];
const newLength = cities.push("Bristol");
console.log(newLength); 

Can anyone clear this confusion of mine?
here, console.log(newLength) gives 3 which made me too confused.
I thought it should return [“Manchester”, “Liverpool”, “Bristol”];

The push method adds items to the end of an array, that’s why your newLength array has three array items.

The first line is an array of items(cities) stored in the cities variable.

The second line is an array called newLength which accesses the cities array and then pushes Bristol to the end.

The third statement is a console log which consoles the newLength array.

array.push() returns its length after modification, not the array itself.

1 Like