Why function does not change the array?

196 views Asked by At

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?

5

There are 5 answers

6
Ayyappan Sekar On

Because you aren't assigning the value returned by myArr() method to the variable. Its should be arr = myArr(arr); rather than myArr(arr); try following,

function myArr(arr){
    arr = arr+arr;
    return arr;
}
var arr = new Array(1,3,2,5);
arr = myArr(arr);
document.write(arr);// It should give Output : 1,3,2,5,1,3,2,5

var arr = new Array(1,3,2,5);
arr = arr+arr;
document.write(arr);// Output : 1,3,2,5,1,3,2,5
1
Daniyal Javaid On

Your function returns the modified array. You called the function myArr(arr) but didnot store the result that your function returned. It should be like

    function myArr(arr){
    arr = arr+arr;
    return arr;
}
var arr = new Array(1,3,2,5);
arr=myArr(arr);
2
void On

There is no pass-by-reference in javascript.

Inside the function myArr, arr has a nested scope so it never took the arr value from the global scope.

function myArr(arr){
    arr = arr+arr; // creates new arr at a different location
    return arr;
}
var arr = new Array(1,3,2,5);
arr = myArr(arr);
document.write(arr);

0
Dhruvin On

function myArr(arr){
    console.log(typeof(arr)); // pass as a Object
    arr = arr+arr;            // + operator do concatination string
    console.log(typeof(arr)); // now converted in String
    return arr;
}
var arr = new Array(1,3,2,5);
myArr(arr);
document.write("Output 1="+arr+"<br/>");// Output : 1,3,2,5

arr = arr+arr;  // + operator do concatination string

// 1,3,2,5 + 1,3,2,5  ==> type String( 1,3,2,51,3,2,5 )

document.write("Output 2="+arr);// Output : 1,3,2,51,3,2,5

0
xianshenglu On

code below is the same as code you give,but it is more easy to understand:

    function myArr(array){
        array = array + array;
        return array;
    }

    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

Look out!!! arr is the global variable while array is the partial variable.

If you change array in myArr to arr,that doesn't matter.

Ppartial variable only exists when the function is running except closure.

Explanation is in the comment:

    var arr = new Array(1,3,2,5);//arr=[1,3,2,5];

    myArr(arr);
    function myArr(array){//array=[1,3,2,5],array===arr;
        array = array + array;//array=1,3,2,51,3,2,5;arr didn't change!!!
        return array;//return 1,3,2,51,3,2,5;arr still didn't change!!!
    }

    document.write(arr);//arr=[1,3,2,5],so you see 1,3,2,5

    arr = arr + arr;//arr=1,3,2,51,3,2,5

    document.write(arr);//so you see 1,3,2,51,3,2,51,3,2,5 because it didn't erase the last content.In this case,document write 1,3,2,51,3,2,5 after 1,3,2,5