0

I am doing a simple javascript problem where I have to pass an array through a function and have the array reassigned a different value. How come when I pass through an array (or even any variable) I have to specifically reassign the array in order for it to change? Can't I just use the code in the function to use the parameter to do that for me? Here's my code:

<script>
var str = 'this is my sentence';
//Write a function called reverse that takes is given str as it's only argument and returns that string after it's been reversed

function reverse(x) {
    x = x.split('').reverse().join('');
    return x;
}

//returns with the text reversed, but str is still 'this is my sentence' and not the reversed version
</script>
  • Where are you calling the reverse function? – Carlos Rodriguez Apr 17 '15 at 05:21
  • I see a definition of reverse(), but where do you invoke it and store the result in str? – Hypaethral Apr 17 '15 at 05:22
  • "Can't I just use the code in the function to use the parameter to do that for me?" - nope. Parameters don't work that way; you can't reassign the caller's variables through a function's arguments. – user2357112 Apr 17 '15 at 05:23
  • See [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) for a more general explanation. – Qantas 94 Heavy Apr 17 '15 at 05:33
  • Also look at this question: http://stackoverflow.com/questions/28148196/how-do-i-get-this-this-in-prototype-working/28148255#28148255 – Qantas 94 Heavy Apr 17 '15 at 05:56
  • This may be helpful to you http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – Abhishek Chauhan Apr 17 '15 at 05:27

1 Answers1

1

You have to actually call the reverse function. Your code works once you add str = reverse(str);

<script>
var str = 'this is my sentence';
//Write a function called reverse that takes is given str as it's only argument and returns that string after it's been reversed

function reverse(x) {
    x = x.split('').reverse().join('');
    return x;
}

str = reverse(str);  //Call the function

window.alert(str);  //show an alert box to view reversed string

</script>

EDIT

It seems the brunt of his question is [Why do] I have to specifically reassign the array in order for it to change?.

The argument is a primitive and with primitives, javascript passes by value. Meaning that the called functions' parameter will be a copy of the callers' passed argument. It is not the same item.

The alternative would be pass by reference in which case the called functions' parameter will be the same object as the caller's passed argument. In this case, changes occurring inside the function on the object passed as a parameter will be 'available' outside the function - because it's the same object. This is how Javascript passes objects.

In Javascript, a string can be either an object or a primitive depending on how you create it:

var str = "I'm a String primitive"; //This is a primitive

var str = new String("I'm a String object"); //this is an object

BranLakes
  • 338
  • 2
  • 15