How to convert two or more words into hyphen ‘-’ separated string in Javascript
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.
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