This is a simple code that I want to add the passed parameters to the array, and then return all the array elements.
function addToArray(param) {
const array = [];
array.push(param);
return array;
}
console.log(addToArray('abc'))
console.log(addToArray(2))
console.log(addToArray('text'))
For each log it returns
[ 'abc' ]
[ 2 ]
[ 'text' ]
But I want it to return
[ 'abc' ]
[ 'abc', 2 ]
[ 'abc', 2, 'text' ]
I know I can do it if the array was outside of the function, but can I do that locally inside the function without making the variable global?