To convert string characters into the lower case in JavaScript; In this tutorial, you will learn about JavaScript string toLowerCase() method and how to convert string characters into lowercase using this method.
JavaScript String toLowerCase() is a built-in method that is used to convert string characters into lowercase.
Javascript String toLowerCase()
JavaScript str.toLowerCase() method converts the characters of a string into lower case characters.
Note that, this method does not affect any of the special characters, digits, and string characters that are already in lowercase.
The following syntax represents the string.toLowerCase() method:
string.toLowerCase();
Here,
- a string which you want to convert into lowercase.
- toLowerCase() is method, which is used to convert string characters into lowercase.
Let’s take a look at the example:
let str = 'A Quick Javascript Tutorial'; console.log(str.toLowerCase()); // output: a quick javascript tutorial
In this example, you can see all the uppercase string characters converted into lowercase with the help of javascript toLowerCase() method.
Javascript toLowerCase() array
How to convert a string to lowercase which is stored in an array?. You can see the following example:
let arr = [ 'Javascript', 'PHP', 'Mysql', 'Sql' ] let str = arr.join('~').toLowerCase() let newArr = str.split('~') console.log(newArr) //Output: ["javascript", "php", "mysql", "sql"]
In this example, we have used Javascript join() method the mixed-case array into a string, after that use toLowerCase() to convert the string into lowercase. And Javascript split() the string back into an array.
TypeError: Cannot read property ‘toLowerCase’ of undefined
In case, you pass the undefined string into toLowerCase() method and then you will get some error. The error looks like this: TypeError: Cannot read property ‘toLowerCase’ of undefined.
The following example:
let str = undefined let res = str.toLowerCase(); console.log(res) // TypeError: Cannot read property ‘toLowerCase’ of undefined.