

- 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
Hyphen string to camelCase string in JavaScript
Suppose, we have a string that contains words separated by hyphens like this −
const str = 'this-is-an-example';
We are required to write a JavaScript function that takes in one such string and converts it into a camelCase string.
For the above string, the output should be −
const output = 'thisIsAnExample';
The code for this will be −
const str = 'this-is-an-example'; const changeToCamel = str => { let newStr = ''; newStr = str .split('-') .map((el, ind) => { return ind && el.length ? el[0].toUpperCase() + el.substring(1) : el; }) .join(''); return newStr; }; console.log(changeToCamel(str));
Following is the output on console −
thisIsAnExample
- Related Questions & Answers
- How to concatenate string vectors separated with hyphen in R?
- Breaking camelCase syntax in JavaScript
- Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
- Converting any case to camelCase in JavaScript
- How to set a string with hyphen and numbers in MySQL varchar?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Converting string to a binary string - JavaScript
- camelCase in Python
- Python Program to Take in a String and Replace Every Blank Space with Hyphen
- Interchanging a string to a binary string in JavaScript
- String to binary in JavaScript
- Camelcase Matching in C++
- Convert Dashes to CamelCase in PHP
- Repeat String in JavaScript?
- Compressing string in JavaScript
Advertisements