0

I'm new to Javascript

The function is not changing the outer variable

function edit(array, letter){
    array = 0;
    letter = 'b';
}
let string = 'a';
let array = [1,0,2];
edit(array, string);
console.log(array, string);// Result [1,0,2] not 0 b
Nine Tails
  • 378
  • 3
  • 15
Akash RP
  • 61
  • 5
  • Please post properly what you want to do – Ashish Nov 03 '20 at 04:50
  • from where variable ```text``` came from? – Ketan Ramteke Nov 03 '20 at 04:51
  • 1
    Yes, this is how parameters work in JavaScript and many other languages. – Code-Apprentice Nov 03 '20 at 04:55
  • 1
    See the top explanation here: https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language, If you were to pass an object and change the internals of the object you would be able to change the params in this way. But when you try strings or array to primitive you end up with pass by value and the outside variable isn't changed. – scrappedcola Nov 03 '20 at 04:57
  • 1
    You try it: let string = 'a'; let array = [1,0,2]; function edit(newArray, letter){ array = newArray; string = letter; } edit(array, string); console.log(0, 'b'); – Huy Ho Nov 03 '20 at 05:03

0 Answers0