The 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 toLowerCase
JavaScript string toLowerCase() is a built-in function that returns the calling string value converted to lower case. The toLowerCase() function 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.
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());
Convert elements of the array to lowercase
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 this tutorial.