{"id":414,"date":"2020-08-21T19:27:36","date_gmt":"2020-08-21T19:27:36","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=414"},"modified":"2020-08-21T19:47:31","modified_gmt":"2020-08-21T19:47:31","slug":"convert-string-to-different-cases-with-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/convert-string-to-different-cases-with-javascript\/","title":{"rendered":"Case Conversion: Convert String to Lower Case, Upper Case and Title Case With JavaScript"},"content":{"rendered":"\n<p><strong>Convert String to Lower Case :<\/strong><\/p>\n\n\n\n<p>In lower case, all letters of string appears in their small forms. Using the inbuilt <code><strong>toLowerCase()<\/strong><\/code> method we can easily convert given text to lower case letters with JavaScript.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var str = \"JavaScript is Awesome\";\nconsole.log(str.toLowerCase()); \n\/\/ Output: javascript is awesome<\/code><\/pre>\n\n\n\n<p><strong>Convert String to Upper Case :<\/strong><\/p>\n\n\n\n<p>In upper case, all letters of string appears in their capital forms  Using the inbuilt <code><strong>toUpperCase()<\/strong><\/code> method a string can be converted to upper case letters with JavaScript.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var str = \"JavaScript is Awesome\";\nconsole.log(str.toUpperCase()); \n\/\/ Output: JAVASCRIPT IS AWESOME<\/code><\/pre>\n\n\n\n<p><strong>Convert String to Title Case :<\/strong><\/p>\n\n\n\n<p>In title case, first letter of every word in the string appears in capital form and rest of the letters remain in small form. There is no inbuilt method to convert a string to title case in JavaScript. But we can do this in various ways. I will show you one simple way, checkout the following example:<\/p>\n\n\n\n<p><a href=\"https:\/\/datatype.co.in\/code-editor\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Try Yourself (opens in a new tab)\">Try Yourself<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function toTitleCase(str) {\n  \/\/ Validate input\n  if(!str) return;\n  \/\/ Split the string into array of words\n  var wordsArr = str.split(' ');\n  var result = [];\n  var word = \"\";\n  for(var i = 0; i &lt; wordsArr.length; i++){\n    \/\/ Lower case each word\n    word = wordsArr[i].toLowerCase(); \n    \/\/ Convert first character to upper case, append the rest\n    result.push(word[0].toUpperCase() + word.slice(1));\n  }\n  \/\/ Convert array of title cased words to space separated string \n  return result.join(' '); \n}\n\nvar str = \"JavaScript is Awesome\";\nvar titleCased = toTitleCase(str);\nconsole.log(titleCased);\n\/\/ Output: Javascript Is Awesome<\/code><\/pre>\n\n\n\n<p>The function is self explanatory. I am using inbuilt array methods <code><strong>split()<\/strong><\/code>, <code><strong>join()<\/strong><\/code> and <code><strong>splice()<\/strong><\/code> to convert given string to title case. If you want to learn more about the array methods read <a rel=\"noreferrer noopener\" aria-label=\"this  (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/important-javascript-array-methods\/\" target=\"_blank\">this<\/a> article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Convert String to Lower Case : In lower case, all letters of string appears in their small forms. Using the inbuilt toLowerCase() method we can easily convert given text to&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[165,167,166,168],"class_list":["post-414","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-case-conversion-javascript","tag-javascript-lower-case-conversion","tag-javascript-title-case-conversion","tag-javascript-upper-case-conversion","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":24,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}