How to convert a string to snake case using JavaScript ? Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are given a string in and the task is to write a JavaScript code to convert the given string into a snake case and print the modified string. Examples: Input: GeeksForGeeks Output: geeks_for_geeks Input: CamelCaseToSnakeCase Output: camel_case_to_snake_caseThere are some common approaches: Table of Content Using match(), map(), join(), and toLowerCase() Using for loopUsing Lodash _.snakeCase() methodUsing match(), map(), join(), and toLowerCase() We use the match(), map(), join(), and toLowerCase() methods to convert a given string into a snake case string. The match() method is used to match the given string with the pattern and then use map() and toLowerCase() methods to convert the given string into lower case and then use join() method to join the string using underscore (_). Example: This example shows the use of the above-explained approach. JavaScript function snake_case_string(str) { return str && str.match( /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(s => s.toLowerCase()) .join('_'); } console.log(snake_case_string('GeeksForGeeks')); console.log(snake_case_string('Welcome to GeeksForGeeks')); console.log(snake_case_string('Welcome-to-GeeksForGeeks')); console.log(snake_case_string('Welcome_to_GeeksForGeeks')); Outputgeeks_for_geeks welcome_to_geeks_for_geeks welcome_to_geeks_for_geeks welcome_to_geeks_for_geeks Using for loopIn this approach first we initialize empty string to store snake string then we Iterate over each character of the input string and check if the current character is uppercase and not the first character of the string. If so, we will add an underscore ('_') before appending the lowercase version of thwill contain the snake case string. Example: below example uses above explained approach. JavaScript function toSnakeCase(str) { let snakeCase = ''; for (let i = 0; i < str.length; i++) { const char = str[i]; if (char.toUpperCase() === char && i > 0) { snakeCase += '_'; } snakeCase += char.toLowerCase(); } return snakeCase; } console.log(toSnakeCase("welcomeToGeeksForGeeks")); Outputwelcome_to_geeks_for_geeks Using Lodash _.snakeCase() methodLodash _.snakeCase() method is used to convert the given string into a snake case string. Example: below example uses _.snakeCase() method to convert a string to snake case. JavaScript const _ = require('lodash'); let myString = 'GeeksForGeeks'; let snakeCaseString = _.snakeCase(myString); console.log(snakeCaseString); Output: geeks_for_geeks Comment More infoAdvertise with us Next Article How to convert a string to snake case using JavaScript ? V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 GeeksOutput: geeks-for-geeksInput: GeeksForGeeksOutput: geeks-for-geeksInput: Geeks_for_geeksOutput: geeks-for-geeksBelow are the approaches 3 min read How to convert an object to string using JavaScript ? To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp 4 min read How to Convert String to Camel Case in JavaScript? We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin 4 min read JavaScript - Convert String to Title Case Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript.1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and t 4 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to check the given string is palindrome using JavaScript ? A palindrome is a word, sentence, or even number that reads the same from the back and from the front. Therefore if we take the input, reverse the string and check if the reversed string and the original string are equal, it means the string is a palindrome, otherwise, it is not. Approach: When the 3 min read JavaScript - Convert Comma Separated String To Array Here are the various methods to convert comma-separated string to array using JavaScript.1. Using the split() Method (Most Common)The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified cha 3 min read JavaScript - Convert String to Array Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:Access individual characters or substrings.Perform array operations su 5 min read Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read Like