0

I try to push a new value into my array. But when I execute my code an integer is show instead my new array.

var foo = ["Hello"].push(" word");
console.log(foo)

2

2 Answers 2

2

You should use concat in this context to achieve your goal.

var foo = ["Hello"].concat(" word");
console.log(foo)

Sign up to request clarification or add additional context in comments.

Comments

2

That is because you are storing the value that push returns on the variable foo:

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

You can do it this way:

var arr = ["hello"];
arr.push(" word");
console.log(arr);

Comments