AppDividend
Latest Code Tutorials

JavaScript String toLowerCase: The Complete Guide

String.toLowerCase() function in JavaScript converts the entire string to lower case. The toLowerCase() function does not affect any of the special characters, digits, and alphabets already in the lower case.

Javascript String toLowerCase()

JavaScript string toLowerCase() is a built-in function that returns the calling string value converted to lower case. The toLowerCase() does not accept any parameter. Instead, the new string representing the calling string is converted to a lower case.

Syntax

string.toLowerCase()

Arguments

The toLowerCase() function does not take any arguments.

Return Value

The toLowerCase() method returns the value of the string converted to lower case. The toLowerCase() function does not affect the value of the string itself.

Example

Let’s define a string using single quotes and then convert it into lowercase.

// app.js

let strA = 'Avengers will be a great movie';
console.log(strA.toLocaleLowerCase());

The toLowerCase()  function does not take any arguments.

It merely returns the new string in which all the upper case letters are converted to the lower case.

Run the file by the following command.

node app

See the output below.

Javascript String toLowerCase Example

This is how you can convert Javascript Strings To Lower Case.

See the following more examples of converting all the string characters to lowercase.

// app.js

console.log('AppDividend'.toLocaleLowerCase());
console.log('KrunalLathiya'.toLowerCase());

String.prototype.toLowerCase() Tutorial

Convert elements of the array to lowercase

How to convert an Array of strings toLowerCase?? Well, see the following code.

To convert an array of elements to lowercase in Javascript, use the combination of join(), toLowerCase(), and split() methods.

let arr = [
  'MILLIE',
  'NOAH',
  'FINN',
  'SADIE'
]

let str = arr.join('~').toLowerCase()
let finArr = str.split('~')
console.log(finArr)

See the output.

➜  es git:(master) ✗ node app
[ 'millie', 'noah', 'finn', 'sadie' ]
➜  es git:(master) ✗

In the above code, we have used the Javascript join() method, the mixed-case array into a string, lowercase it, then Javascript split() the string back into an array.

The above method could cause performance issues if you have a large amount of data in the array.

TypeError: Cannot read property ‘toLowerCase’ of undefined

If you pass the string as an undefined, then you will get this TypeError: Cannot read property ‘toLowerCase’ of undefined.

See the following code.

let str = undefined

let res = str.toLowerCase()
console.log(res)

 See the output.

➜  es git:(master) ✗ node app
/Users/krunal/Desktop/code/node-examples/es/app.js:3
let res = str.toLowerCase()
              ^

TypeError: Cannot read property 'toLowerCase' of undefined
    at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:3:15)

That’s it for string tolowercase() function example.

Recommended Posts

Javascript String toUpperCase()

Javascript String toLocaleUpperCase()

Javascript String toLocaleLowerCase()

Javascript String replace()

Javascript String charCodeAt()

Javascript String trimStart() trimEnd()

Javascript String includes()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.