Work with our skilled Javascript developers to accelerate your project and boost its performance.
Hire JavaScript DevelopersYou can split a JavaScript string using the `split` method, which is part of the String prototype. In your case, since the string is formatted with hyphens (`-`), you can split it by specifying `’-‘` as the delimiter. Here’s how you can do it:
Using an Array
var coolVar = '123-abc-itchy-knee'; var array = coolVar.split('-'); console.log(array[0]); // '123' console.log(array[1]); // 'abc' console.log(array[2]); // 'itchy' console.log(array[3]); // 'knee'
Using Separate Variables
var coolVar = '123-abc-itchy-knee'; var [var1, var2, var3, var4] = coolVar.split('-'); console.log(var1); // '123' console.log(var2); // 'abc' console.log(var3); // 'itchy' console.log(var4); // 'knee'