How to usefor loop in Javascript

In this approach we iterate over each character in the string using for loop. For each character, if it’s uppercase and not the first character, add a hyphen before converting it to lowercase. Replace spaces, underscores with hyphens. Append all other characters directly to the new string.

Example: In this example, we are using the above-explained approach.

JavaScript
function toKebabCase(str) {
    let kebabCase = '';
    for (let i = 0; i < str.length; i++) {
        const char = str[i];
        if (char.toUpperCase() === char && char.toLowerCase() !== char) {
            if (i > 0) {
                kebabCase += '-';
            }
            kebabCase += char.toLowerCase();
        } else if (char === ' ' || char === '_' || char === '-') {
            kebabCase += '-';
        } else {
            kebabCase += char;
        }
    }
    return kebabCase;
}

console.log(toKebabCase("welcomeTow3wiki"));

Output
welcome-to-geeks-for-geeks


How to convert a string into kebab case using JavaScript ?

Given a string with space-separated or camel case or snake case letters, the task is to find the kebab case of the following string. 

Examples:

Input:  Geeks For Geeks
Output: geeks-for-geeks
Input: w3wiki
Output: geeks-for-geeks
Input: Geeks_for_geeks
Output: geeks-for-geeks

Below are the approaches used to convert a string into a kebab case using JavaScript:

Table of Content

  • Approach 1: Using the replace() method
  • Approach 2: Using the match() method
  • Approach 3: Using Lodash _.kebabCase() method
  • Approach 4: Using for loop

Similar Reads

Approach 1: Using the replace() method

Here we have a function named kebabCase which takes a string and returns a string after converting the kebab case. Here we are using replace method two times because the first replace method is to get all the letters that are near to the uppercase letters and replace them with a hyphen. The second replace function is used for getting the spaces and underscores and replacing them with a hyphen....

Approach 2: Using the match() method

Here, we use the map method that checks for space, capital letters, and underscores. It creates an array and pushes the words that separate the strings. Now join the array with the hyphen using the join(). After that convert the whole string into a lower case....

Approach 3: Using Lodash _.kebabCase() method

Lodash _.kebabCase() method is used to convert the given string into a kebab case string. kebabCase is the practice of writing identifiers using hyphens instead of spaces. The string can be space-separated, dash-separated, or can be separated by underscores....

Approach 4: Using for loop

In this approach we iterate over each character in the string using for loop. For each character, if it’s uppercase and not the first character, add a hyphen before converting it to lowercase. Replace spaces, underscores with hyphens. Append all other characters directly to the new string....

Contact Us