How to convert two or more words into hyphen ‘-’ separated string in Javascript

JS Developers
2 min readFeb 1, 2021

--

We’ll discuss, By using Javascript how we can join two or more strings in Hyphen(-) separated string.

Many times we want to change a sentence(two or more words) into a hyphen(-) separated string to make it classname in React or in many other cases. Javascript has a property called Join and split which helps to solve the above problem very efficiently.

Photo by Artem Sapegin on Unsplash

Let’s start to solve it.

Suppose We have a word “Hello World” and need to convert it into the string “hello-world”.

firstly we need to join it but join in javascript only works with the array. so to convert the string “Hello world” into the array as [“Hello”, “world”] we need to use the split function.

Now we can join it as following..

to convert it into the lower case we can use the toLowerCase property of javascript.

Yeah!!! we got the Solution.

Thanks

Have any other doubts…..

Contact me on LinkedIn,or Git for any related query of javascript or its framework/librabries. we’ll definitely solve it together.

--

--

JS Developers
JS Developers

No responses yet