Your function has a few issues that can be improved. Firstly, the line of code myArray;
at the end is unnecessary as it does not serve any useful purpose. Secondly, the variable arrayLength
is defined but not used in the function. Lastly, the variable myArray
is redundant and can be removed from the function. The usage of unshift
and push
seems fine.
A revised version of your function could look like this:
var addTwo = function(array) {
array.unshift(1);
array.push(1);
return array;
};
The use of return array
is necessary only if the caller does not already have access to the modified array.
Here are some examples of how to use the function:
var a = ["apple","orange","banana"];
addTwo(a);
console.log(a); // [1, "apple", "orange", "banana", 1]
and
var a = addTwo(["apple","orange","banana"]);
console.log(a); // [1, "apple", "orange", "banana", 1]