1

I have a variable that I pass into a function, and return its updated value. The syntax looks very similar to:

var myName = 'hello all.';

FuncUpperCase(myName);

function FuncUpperCase(myName) {
     myName = myName.toUpperCase();
     return myName;
}

But the variable myName doesn't seem to be modified with the value from toUpperCase().

Note: I don't want to write var newName = FuncUpperCase(myName), because I don't like that syntax.

Bram
  • 2,515
  • 6
  • 36
  • 58
  • @Goodwine He wants a string to be in uppercase. He just didn't put his returned value in a var that's why it isn't working. This is not a duplicate of that post. – Bram May 21 '15 at 09:52

2 Answers2

1

This might answer your question:

Pass a string by reference in Javascript

Alternatively, pass an object and not a string.

var something = {};
something.word = "word";
var allcaps = function(inp) {inp.word = inp.word.toUpperCase();};

allcaps(something);

//something.word has now been changed
alert(something.word);
Community
  • 1
  • 1
Vytas Bradunas
  • 626
  • 1
  • 7
  • 15
  • This solution sounds more "js-ish" to me so I upvoted it – pinturic May 21 '15 at 10:03
  • Thank you, your code is good. Now extend this problem, how can Lowercase() key object something (not key value) follow that syntax. Please help me !!! – Đình Nhơn Lê May 22 '15 at 01:57
  • @ĐìnhNhơnLê You should make a new question for that. – Bram May 22 '15 at 05:59
  • @ĐìnhNhơnLê This post answers how to change the property name on the object. You can't actually change it. Create a new property and delete the old one. http://stackoverflow.com/questions/8483425/change-property-name – Vytas Bradunas May 22 '15 at 06:37
  • @ĐìnhNhơnLê Aslo, happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – Vytas Bradunas May 22 '15 at 06:45
0

In JavaScript, everything is passed by value. It means that if you reassign a value for a given parameter in a function, you will not modify the value outside of the scope of the given function. Another important thing to understand, is how value to object references work: if you pass an object to a function, it is also by value that it is given, but if you access a property of this object, then the actual object will be modified outside of the function too, but NOT the value (that is to say, the value which is a reference to the object).


In the end you have to pass an object to your function and modify a property of it to make it persist outside.

function setToUpperCase(object, key) {
    object[key] = ('' + object[key]).toUpperCase();
    return object[key];
}

And use it like this:

var holder = { word: 'blah' };
setToUpperCase(holder, 'word');
console.log(holder.word); // BLAH
axelduch
  • 10,769
  • 2
  • 31
  • 50
  • 1
    `everything is passed by value` : false, in your example `holder` is passed by reference... – Hacketo May 21 '15 at 10:29
  • 1
    No it is passed by value, as I said, it happens that the value is a reference to the object. Try to do `object = {};` it will not change the value of `object`, ever, it will change the value that was the reference to `object`. – axelduch May 21 '15 at 10:33