Don
Don

Reputation: 193

toUpperCase() is not making the string upper case

I don't know what I'm doing wrong; But somehow .toUpperCase() String-function is not working on my browser or do I get something wrong?

var string ="kjsdgfiIJHBVSFIU";
string.toUpperCase();
console.log(string);

Live demo

Upvotes: 8

Views: 40817

Answers (9)

ssilas777
ssilas777

Reputation: 9754

String is Immutable. Once created, a string object can not be modified.

So here toUpperCase returns a new string, This should work-

var string ="kjsdgfiIJHBVSFIU";
var newString = string.toUpperCase();
alert(newString);

Upvotes: 8

khizerrehandev
khizerrehandev

Reputation: 1525

  • You missed to hold the updated data in string key
  • It's not a good convention to have name like string can be upperCaseString or newString or updatedString. That might be for testing purpose but try to follow good convention :) It Will help in future.
var string ="kjsdgfiIJHBVSFIU";
string = string.toUpperCase();
console.log(string);

// Output:
KJSDGFIIJHBVSFIU

Upvotes: 1

Jericho
Jericho

Reputation: 174

I didn't know that string is unchangeable unless your reassign it with new name

console.log(i, 'is a ' + typeof i);
var N = i.toUpperCase();
console.log(N);

Upvotes: 1

ganeshprasad
ganeshprasad

Reputation: 11

Convert to string value to toUpperCase And toLowercase then object with method().

var string ="kjsdgfiIJHBVSFIU";

console.log(string.toUpperCase());

Upvotes: 0

Teknia
Teknia

Reputation: 151

Previous answers about Strings being immutable are great! Here is just another potential run-time reason for getting this error which might be hard to spot at first. It does not detract from any of the above valid answers:

If the object on which the .toUpperCase() method is invoked is not a String, then the runtime doesn't 'find' the method toUpperCase() since that method/function only exists on String objects.

e.g. console.log(variableName.toUpperCase());

If variableName is of the type String, it works fine. If it is of another type (that does not have a toUpperCase() method, it says 'toUpperCase() is not a function (since it isn't).

If you know it should be a string, you can cast it like this:

console.log(String(variableName).toUppercase());

Hope it helps.

Upvotes: 1

Topguru
Topguru

Reputation: 21

var string ="kjsdgfiIJHBVSFIU";
string = string.toUpperCase();
console.log(string);

Upvotes: 1

bcoughlan
bcoughlan

Reputation: 26627

toUpperCase returns the new string, so you must write:

string = string.toUpperCase();

In many languages, strings are immutable, meaning that they can not be modified once created. While this costs in efficiency, it is important for object oriented programming, because if a String passed by reference to a function was modifiable, the state of objects could be changed without the object's consent.

Upvotes: 3

yorlin
yorlin

Reputation: 440

var upperCase = string.toUpperCase();
console.log(upperCase);

toUpperCase doesn't transform existing string, it just returns a uppercase string.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

.toUpperCase returns the upper-cased string. It is not an in-place modifier method.

string = string.toUpperCase();

Documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/toUpperCase

Upvotes: 29

Related Questions