- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Changing the case of a string using JavaScript
We are required to write a JavaScript function that takes in a string and converts it to snake case.
Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.
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
The output in the console will be −
this_is_a_simple_sentence
- Related Articles
- Creating permutations by changing case in JavaScript
- Changing second half of string number digits to zero using JavaScript
- Swapping string case using a binary number in JavaScript
- How to convert a string into the lower case using JavaScript?
- How to convert a string into upper case using JavaScript?
- Casting a string to snake case - JavaScript
- Convert mixed case string to lower case in JavaScript
- How match a string irrespective of case using Java regex.
- PHP – Case folding in a string using mb_convert_case()
- PHP – Make a lower case string using mb_strtolower()
- Changing an array in place using splice() JavaScript
- How to convert a string to camel case in JavaScript?
- Change the case without using String.prototype.toUpperCase() in JavaScript
- Changing the look of Cursor using CSS
- Java Program to swap the case of a String

Advertisements