

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Converting strings to snake case in JavaScript
Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.
We are required to write a JavaScript function that takes in a string and converts it to snake case.
Example
The code for this will be −
const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => { const strArr = str.split(' '); const snakeArr = strArr.reduce((acc, val) => { return acc.concat(val.toLowerCase()); }, []); return snakeArr.join('_'); }; console.log(toSnakeCase(str));
Output
Following is the output on console −
this_is_a_simple_sentence
- Related Questions & Answers
- Casting a string to snake case - JavaScript
- Converting any case to camelCase in JavaScript
- Converting strings to numbers with vanilla JavaScript
- Write down a python function to convert camel case to snake case?
- Converting to upper case in Java.
- Converting strings to uppercase and lowercase with vanilla JavaScript
- Converting Strings to Numbers in C/C++
- How to do case insensitive string comparison of strings in JavaScript
- Converting any string into camel case with JavaScript removing whitespace as well
- Converting all strings in list to integers in Python
- Converting Strings to Numbers and Vice Versa in Java.
- Converting digits to word format using switch case in C language
- JavaScript filter an array of strings, matching case insensitive substring?
- Converting array to set in JavaScript
- Converting degree to radian in JavaScript
Advertisements