<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: maha0134</title>
    <description>The latest articles on DEV Community by maha0134 (@maha0134).</description>
    <link>https://dev.to/maha0134</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F727667%2F508a80d9-9219-4534-b58d-91790c582275.png</url>
      <title>DEV Community: maha0134</title>
      <link>https://dev.to/maha0134</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maha0134"/>
    <language>en</language>
    <item>
      <title>String.prototype .toUpperCase() vs String. prototype.toLocaleUpperCase()</title>
      <dc:creator>maha0134</dc:creator>
      <pubDate>Sat, 16 Oct 2021 23:58:05 +0000</pubDate>
      <link>https://dev.to/maha0134/stringprototype-touppercase-vs-string-prototypetolocaleuppercase-36ma</link>
      <guid>https://dev.to/maha0134/stringprototype-touppercase-vs-string-prototypetolocaleuppercase-36ma</guid>
      <description>&lt;h2&gt;
  
  
  Understanding String.toUpperCase()
&lt;/h2&gt;

&lt;p&gt;The string.toUpperCase() method is used to convert a string into upper case letters. So, if we pass the string &lt;code&gt;hello, my name is john doe&lt;/code&gt; through the string.toUpperCase method, it should return &lt;code&gt;HELLO, MY NAME IS JOHN DOE&lt;/code&gt; as the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The method simply returns the string in upper case letters but doesn't change the original string. Our example shall make this clear.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string.toUpperCase()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Let's check with our string &lt;code&gt;hello my name is john doe&lt;/code&gt; how this works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sentence = 'hello my name is john doe'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;we assigned our string to a variable called &lt;strong&gt;sentence&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let upperCase = sentence.toUpperCase();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;we declared a new variable &lt;strong&gt;upperCase&lt;/strong&gt; and assigned the return value of the method to it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's print this to the console and see the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(upperCase);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The output comes as expected,&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;HELLO MY NAME IS JOHN DOE&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can simply output the result without the need for declaring a new variable too, check the code below in case you need to simply output a given string into &lt;code&gt;UPPERCASE&lt;/code&gt; letters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sentence = "hello my name is john doe";
console.log(sentence.toUpperCase());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The output is still the same,&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;HELLO MY NAME IS JOHN DOE&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's try to see if this method affects our original string variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sentence = "hello my name is john doe";
let upperCase = sentence.toUpperCase();
console.log(upperCase, sentence);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The output comes to&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;HELLO MY NAME IS JOHN DOE hello my name is john doe&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As is clear from the above example, this method simply returns a value but doesn't alter the string in question.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exceptions for String.toUpperCase() method
&lt;/h3&gt;

&lt;p&gt;This method shall work on all &lt;code&gt;strings&lt;/code&gt; but what if the input is not &lt;code&gt;string&lt;/code&gt;. If we try this method with, let's say a &lt;code&gt;number&lt;/code&gt; or a &lt;code&gt;boolean&lt;/code&gt; value like &lt;strong&gt;&lt;em&gt;true&lt;/em&gt;&lt;/strong&gt; or if we simply used an &lt;code&gt;undefined&lt;/code&gt; variable, it shall give a &lt;strong&gt;&lt;em&gt;type error&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Clearly, the uses for this method are countless. If we are dealing with strings, we could use this method and get our inputs converted into &lt;code&gt;UPPERCASE&lt;/code&gt; letters with ease. This method can work in conjunction with a &lt;code&gt;for&lt;/code&gt; loop and we can even convert the string elements of an array into UPPERCASE letters. &lt;/p&gt;

&lt;p&gt;Check the code below and try it in your browsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sampleArray = ['newyork', 'ottawa', 'toronto', 'washington dc']
sampleArray.forEach(element =&amp;gt; console.log(element.toUpperCase()));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding String.toLocaleUpperCase()
&lt;/h2&gt;

&lt;p&gt;The String.toLocaleUpperCase() method returns a string input after converting it to either a default or a specified locale.&lt;/p&gt;

&lt;p&gt;Let's first try to understand what &lt;strong&gt;&lt;em&gt;Locale&lt;/em&gt;&lt;/strong&gt; means. &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.merriam-webster.com/dictionary/locale"&gt;Merriam-Webster&lt;/a&gt; dictionary defines locale as '&lt;em&gt;a place or locality especially when viewed in relation to a particular event or characteristic&lt;/em&gt;'. So, in Javascript that would translate to the locale/locality of the host server by default or any other user-defined locale (like "en-US", "en-CA"). &lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String.toLocaleUpperCase()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;Let's look at a few examples to see how String.toLocaleUpperCase() method works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let language = 'turkish';//#1
console.log(language.toLocaleUpperCase());//#2
console.log(language.toLocaleUpperCase('en-CA'));//#3
console.log(language.toLocaleUpperCase('TR'));//#4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we look at the output of this code, let's try to understand what it does. &lt;br&gt;
&lt;strong&gt;&lt;em&gt;#1&lt;/em&gt;&lt;/strong&gt; assigns the &lt;code&gt;string&lt;/code&gt; value '&lt;em&gt;turkish&lt;/em&gt;' to the variable &lt;strong&gt;&lt;em&gt;language&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;#2&lt;/em&gt;&lt;/strong&gt; prints out the &lt;code&gt;uppercase&lt;/code&gt; version of '&lt;em&gt;turkish&lt;/em&gt;' in the locale of the host server(let's assume we are working in the US, so the locale is "en-US" or American English).&lt;br&gt;
&lt;strong&gt;&lt;em&gt;#3&lt;/em&gt;&lt;/strong&gt; prints out the &lt;code&gt;uppercase&lt;/code&gt; version of '&lt;em&gt;turkish&lt;/em&gt;' in the user-specified Canadian English locale.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;#4&lt;/em&gt;&lt;/strong&gt; prints out the &lt;code&gt;uppercase&lt;/code&gt; version of '&lt;em&gt;turkish&lt;/em&gt;' in the user-specified Turkish locale.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The output shall look like this&lt;br&gt;
&lt;br&gt;
&lt;code&gt;TURKISH TURKISH TURKİSH&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;p&gt;Notice something different with the output of &lt;strong&gt;&lt;em&gt;#4&lt;/em&gt;&lt;/strong&gt;? The dot on the &lt;strong&gt;&lt;em&gt;I&lt;/em&gt;&lt;/strong&gt; is due to the fact that we selected to convert the string into the Turkish locale and the capital I in Turkish is written as &lt;code&gt;İ&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also pass an array of locales through this method and it shall use the &lt;a href="https://tc39.es/ecma402/#sec-bestavailablelocale"&gt;best available locale&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exceptions for String.toLocaleUpperCase() method
&lt;/h3&gt;

&lt;p&gt;This method just like the String.toUpperCase() method shall work on all &lt;code&gt;strings&lt;/code&gt; but if the input is &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt; or an &lt;code&gt;undefined&lt;/code&gt; variable, it shall give a &lt;strong&gt;&lt;em&gt;type error&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  String.toUpperCase() vs String.toLocaleUpperCase()
&lt;/h2&gt;

&lt;p&gt;Now that we saw the usage of both the String.toUpperCase() as well as the String.toLocaleUpperCase() methods, let's look at the differences in between them.&lt;br&gt;
The String.toUpperCase() is not locale specific, so it does not expect any input for the locale. However, even though by default the String.toLocaleUpperCase() method doesn't demand a value to be passed, it is still working and converting letters as per a locale.&lt;/p&gt;

&lt;p&gt;So, practically, for a user using a locale that has all the UPPERCASE alphabets looking like the english alphabets, both these methods shall give the same output and the user can use these methods interchangeably.&lt;/p&gt;

&lt;p&gt;However, if we are working on a language like Turkish or any other language that has characters looking like 'GESÄSS', we will need to use the String.toLocaleUpperCase() method.&lt;/p&gt;

&lt;p&gt;For detailed information on these methods, please refer the MDN Web Docs for &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase"&gt;String.prototype.toUpperCase()&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase"&gt;String.prototype.toLocaleUpperCase()&lt;/a&gt; methods.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
