Jerry
Jerry

Reputation: 1583

Javascript - convert value of object to uppercase

I have an object in my Angular.io or IONIC

{name:"food panda", city:"selangor", phone:"0123456789"}

and use the following code but it's not working:

this.restaurant.name.toUpperCase();

I need to convert food panda to FOOD PANDA.

How can I do that?

Upvotes: 2

Views: 6749

Answers (3)

Scott
Scott

Reputation: 1922

You have to save the resulting value, toUpperCase() does not modify anything.

var d = {name:"food panda", city:"selangor", phone:"0123456789"};
var name = d.name.toUpperCase();

Working JSFiddle

Upvotes: 0

Abhijit Kar ツ
Abhijit Kar ツ

Reputation: 1732

toUpperCase returns a new String, it doesn't modify the original

var restaurant = { name: "food panda", city: "selangor", phone: "0123456789" };

restaurant.name.toUpperCase();

console.log(restaurant.name);           // output: food panda

var restaurantName = restaurant.name.toUpperCase();

console.log(restaurantName);            // output: FOOD PANDA

Upvotes: 6

tylerwgrass
tylerwgrass

Reputation: 656

toUpperCase() creates a copy of the string and changes that one to uppercase. If you want the original string to be changed, you will need to reassign the string:

this.restaurant.name = this.restaurant.name.toUpperCase();

Upvotes: 2

Related Questions