Pass array to function with .slice and .push together
Solution 1:
Simply do:
func([...arr, num]);
Worth noting that Array.push
returns the length of the array anyway, so the first example is irrelevant.
Solution 2:
You would call Array.prototype.concat
instead of Array.prototype.push
.
Also, since concat
already merges two arrays and returns a new one, you so not even need to slice the original array.
const arr = [ 0, 1, 2, 3, 4 ];
const num = 5;
const func = (arr) => arr.map(e => String.fromCharCode(e + 65));
const x = func(arr.concat(num));
console.log(x);
Notes
[ ...arr, val ]
is syntactic sugar for arr.concat(val)