DEV Community

Discussion on: How to get a particular word from a long string in Javascript

Collapse
 
benjaminreid profile image
Benjamin Reid

Nice one! 👍🏻 You can also use array destructuring for something like this too.

const [, newCity] = city.split("/")
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

☝ What he said. Although:

const newCity = city.split("/")[1]
Enter fullscreen mode Exit fullscreen mode

is slightly shorter, and arguably more readable.

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

I agree. Although my aim here was to try out slice() on strings.