Consider below JavaScript code:
function myArr(arr){
arr = arr + arr;
return arr;
}
var arr = new Array(1,3,2,5);
myArr(arr);
document.write(arr); // Output : 1,3,2,5
arr = arr + arr;
document.write(arr); // Output : 1,3,2,51,2,5
Why function myArr() is returning the same array while we are performing the same operation inside and outside the function?
Why two different behaviors are showing here with the same operation statement?
Because you aren't assigning the value returned by myArr() method to the variable. Its should be
arr = myArr(arr);rather thanmyArr(arr);try following,