0

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?

Praveen Soni
  • 771
  • 8
  • 21
user3760959
  • 457
  • 1
  • 6
  • 18

5 Answers5

0

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
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
  • Thanks. But now the arr = arr+arr is giving 1,3,2,51,3,2,51,3,2,51,3,2,5 as output. – user3760959 Feb 12 '18 at 07:55
  • Thats expected as you are doing both arr = myArr(arr); and arr = arr+arr; on the same array. Use either one of it! – Ayyappan Sekar Feb 12 '18 at 07:56
  • Look at my updated answer, I have reinitialized the array after the first execution and it should give you the expected result. – Ayyappan Sekar Feb 12 '18 at 07:59
  • Thanks. I got that. But I'm still confused for one thing. Why return statement in function does't alters the original array? Why return value need to be stored in some variable ? – user3760959 Feb 12 '18 at 08:19
  • Yeah, Its because, the modifications what you do to a variable inside the function doesn't modify the original variable as there is no concept of pass by reference in javascript like in java or php. So, you got to return the modified value and assign it to back to the original variable. Can you accept the answer if it helps? – Ayyappan Sekar Feb 12 '18 at 08:23
  • https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value this can help you to understand passing by reference and passing by value – Ayyappan Sekar Feb 12 '18 at 08:25
0

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);
Daniyal Javaid
  • 1,426
  • 2
  • 18
  • 32
  • But aren't arrays passed as pass-by-reference in JavaScript? If they are passed as reference, then shouldn't function modifies the original array passed as reference? – user3760959 Feb 12 '18 at 08:54
0

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);
void
  • 36,090
  • 8
  • 62
  • 107
  • But aren't arrays in JavaScript are pass-by-reference arguments in functions? – user3760959 Feb 12 '18 at 08:48
  • @user3760959 In javascript primitive types are passed as value and non-primitive types are also passed as value but here value is reference to its location. So when you are doing `arr=arr+arr` it is allocating new memory to `arr` and this is not modifying the original array but if you would do `arr.push(3)` then it will modify the original array. Hope this clarifies your doubt. – void Feb 12 '18 at 09:14
0

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
Dhruvin
  • 164
  • 7
0

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
xianshenglu
  • 4,943
  • 3
  • 17
  • 34