Array.push() and its end result

Array.push() and its end result


Here is a code snippet I created:

var arr = [1,2];
var arr2 = ["a","b","c"];  
var newArr = [...arr]; 
var anotherArr = arr.push(3);
var aloneArr = arr2.push([3]);  

console.log(newArr);        //[1,2]
console.log(anotherArr); //3
console.log(arr);  // [1,2,3]
console.log(aloneArr);  // 4
console.log(arr2); //["a","b","c",[3]]

according to the output of the following code, can I have the conclusion that : arr.push(3) will give you the total number of elements inside anotherArr instead of the end result a.k.a [1,2,3]

same thing goes for aloneArr it will return the number of elements inside aloneArr which is now 4

and is the value of arr2 correct? which means I added a single value array inside of arr2


hi there,
push(arg) adds the given argument to the end of the array it is called on and returns the new length of the array after pushing the argument.
so in your case:
anotherArr = arr.push(3);
here you are storing the new length of arr to variable “anotherArr”. Same for “aloneArr”.
And yes , arr2 is correct, you pushed single valued array .

for reference see link:


thanks for the confirmation!!