One of the most common use cases for arrays and functions is passing an array as a parameter to a function. This allows us to modify the array directly inside the function. This can be useful when we want to perform operations that change the contents of the array.
In the above example, the function
doubleElements takes an array
numbers as a parameter and doubles each element in the array using a
for loop. Notice that when we call the function
doubleElements and pass in the
numbersArr array, the array is modified directly.
Let's change the
num to it.
doubleNumbers below with the second element of the
numbers array and assign it to a new variable called
Print the new
doubledNum variable to the console.
Sign up to start coding
Already have an account?