
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Casting a string to snake case - 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
Following is the code −
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 in the console −
this_is_a_simple_sentence
- Related Articles
- Converting strings to snake case in JavaScript
- Write down a python function to convert camel case to snake case?
- Convert mixed case string to lower case in JavaScript
- How to convert a string to camel case in JavaScript?
- How to convert a string into upper case using JavaScript?
- Type casting in JavaScript.
- Changing the case of a string using JavaScript
- How to convert a string into the lower case using JavaScript?
- Swapping string case using a binary number in JavaScript
- How to do case-sensitive string comparison in JavaScript?
- MySQL automatic string to integer casting in WHERE clause to fetch a specific id
- Convert a C++ String to Upper Case
- How to do case insensitive string comparison of strings in JavaScript
- Snake
- Java String to Lower Case example.

Advertisements