Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm currently studying javascript now. I'm just starting off with string manipulation but I'm kinda confused about this .toLowerCase and .toUpperCase

JavaScript
const Name = "john";
const Age = 19;

let sentence2 = `My name is ${Name} and my age is ${Age}`;
console.log(sentence2.toLocaleUpperCase(sentence2));;


when I use the .toUppercase it displays all the letters as uppercase but with .toLocaleUpperCase it gives me this error

Uncaught RangeError: Incorrect locale information provided
at String.toLocaleUpperCase (<anonymous>)

What I have tried:

I tried to google the error and search this over the internet but all it says is that .toLocaleUpperCase has something to do with different local languages likewise with .toLocaleLowerCase But what I'm trying to manipulate is an English characters.

My hunch is I'm getting this error because I concatenate the Name and Age variable inside and then I tried to turn it to uppercase or lowecase?
Posted
Updated 28-Oct-20 21:08pm
v2
Comments
[no name] 29-Oct-20 1:12am    
You sample code has nothing to do with your question.
lelouch_vi 2 29-Oct-20 3:08am    
hello sorry, I updated the question and edit the code.

1 solution

Ignoring that you don't show us the exact code you used to get the error, but if you get an error message you don't understand, google it: Uncaught RangeError: Incorrect locale information provided at String.toLocaleUpperCase - Google Search[^]
The first link explains why you get the error: String.prototype.toLocaleUpperCase() - JavaScript | MDN[^]
Either you are specifying an invalid locale when you call it, or your browser Locale setting is wrong.

It's entirely possible you are trying to call toLocaleUppercase without reading the documentation and passine the string to be converted as the function parameter - but we can;t see your code, so we will never know ...


Quote:
hello sorry, my bad. I forgot to edit the code after I pasted it. It's now updated.


And what did I say?
"It's entirely possible you are trying to call toLocaleUppercase without reading the documentation and passine the string to be converted as the function parameter"

That's exactly what you are doing:
JavaScript
sentence2.toLocaleUpperCase(sentence2)
If you look at the documentation, the parameter is an optional Locale string to specify exactly which Locale to use, overriding the browser setting. You don;t pass your string to convert!

Try:
JavaScript
console.log(sentence2.toLocaleUpperCase());
 
Share this answer
 
v2
Comments
lelouch_vi 2 29-Oct-20 3:08am    
hello sorry, my bad. I forgot to edit the code after I pasted it. It's now updated.
OriginalGriff 29-Oct-20 3:22am    
Never edit code after you have pasted it: copy and paste the actual code you tested, or you risk sending us off down a blind alley by making a further mistake when typing the code you think you used!
lelouch_vi 2 30-Oct-20 21:49pm    
Hi I'm sorry, I get it now thanks for this
You don;t pass your string to convert!. I tend to overlook things when I don't understand something in my code then when I wake up the next day I kinda get it with ease. Thanks!
OriginalGriff 31-Oct-20 3:45am    
:laugh:
Yeah I know that feeling - I tend to read what I meant to write rather than what I actually did ... :O
OriginalGriff 29-Oct-20 3:26am    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900