JavaScript

How to Make First Letter Lowercase in JavaScript

While programming in JavaScript, we come across situations where it is required to include multiple string values to be edited or in the case of differentiating the actual name from the surname. In such cases, JavaScript provides a very convenient functionality to make the first letter lowercase which handles such types of preprocessing cases.

This manual will guide you to lowercase the first letter in JavaScript.

How to Make First Letter Lowercase in JavaScript?

To make the first letter lowercase in JavaScript, you can use:

We will now go through each of the approaches one by one!

Method 1: Make First Letter Lowercase in JavaScript Using toLowerCase() and slice() Methods

The “toLowerCase()” method converts the given string to lowercase letters, and the “slice()” method returns selected elements as a new array. These methods can be utilized to access the index of a particular character, convert it into lowercase and merge it into the string.

Syntax

string.slice(start, end)

In the given syntax, “start” and “end” refers to the start and end indexes of the given string.

The following example explains the stated concept.

Example

In the following example, store a particular string value in a variable named “string” and display it:

const string= "FIRST LETTER LOWERCASE"

console.log("The default string value is:", string)

Now, apply the “charAt()” method in order to refer to the specific index “0” representing the first element, the toLowerCase() method to transform the character at the specified index to lowercase and the slice() method to place the character at the start of the string by referring to its index “1”:

const string1= string.charAt(0).toLowerCase() + string.slice(1)

Finally, display the updated string value with the first letter as lowercase on the console:

console.log("The updated string value is:", string1)

The output of the above implementation will be as follows:

Method 2: Make First Letter Lowercase in JavaScript Using Logical AND Operator and toLowerCase() method

The “Logical AND” Operator (&&) returns true if both the values are true and false otherwise and the This operator can be used in combination with toLowerCase() to perform the AND operation between both the original string value and the value holding the first-string character as lower case and return the result.

Example

First, declare a function named “firstLower()” containing “lower” as an argument referring to the string value to be checked upon on the function call. In its definition, apply the “&&” operator, which will access the original string value along with the updated string value. Here, the toLowerCase() and slice() methods will convert the first-string character into the lower case and merge it into the given string value respectively as discussed in the previous method:

function firstLower(lower){

return lower && lower[0].toLowerCase() + lower.slice(1) || lower;

}

Finally, invoke the defined “firstLower()” function and pass the required value in it:

console.log(firstLower('JAVASCRIPT'));

Output

Method 3: Make First Letter Lowercase in JavaScript Using Regular Expression and replace() method

A “regular expression” is a sequence of characters that form a search pattern and the “replace()” method searches for a specific value in the given string and replaces it. These methods can be implemented to do a global search for the first-string character and transform it into the lowercase.

Syntax

/pattern/modifiers;

Here, “pattern” is a regular expression and “modifiers” refer to a modifier.

string.replace(searchValue, newValue)

Here, “searchValue” will be replaced with the “newValue” in the given string.

The below example explains the stated concept.

Example

Firstly, define a function named “firstLower()” with “string” as an argument referring to the string value that needs to be checked. Then, specify a regular expression for searching the first-string character globally using the “/g” flag and converting it into lowercase using the toLowerCase() method.

function firstLower(string){

return string.replace(/(?:^|\s)\S/g, function(a){

return a.toLowerCase(); });

};

Finally, invoke the function by placing the required as an argument to it:

console.log(firstLower("CASE"))

Output

We have discussed different creative methods to make the first letter lowercase in JavaScript.

Conclusion

To make the first letter lowercase in JavaScript, utilize the “toLowerCase()” and “slice()” methods for transforming the first string character into lower case and merging in the original string, the “Logical AND” Operator and the “toLowerCase()” method to “AND” the original string value and the value holding the first character as the lower case with the help of a function or the “Regular Expression” and “replace()” method to do a global search for the first character in a string and transforming it into lower case. This write-up demonstrated the methods to lowercase the first letter in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.