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.

1
  • @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. Commented May 21, 2015 at 9:52

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

This solution sounds more "js-ish" to me so I upvoted it
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 !!!
@ĐìnhNhơnLê You should make a new question for that.
@Đì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. stackoverflow.com/questions/8483425/change-property-name
@Đì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.
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

2 Comments

everything is passed by value : false, in your example holder is passed by reference...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.