Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
October 16, 2024
You 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'