DEV Community

Discussion on: 10 Awesome JavaScript String Tips You Might Not Know About

Collapse
 
machineno15 profile image
Tanvir Shaikh • Edited

For point 6.
whats the difference between
word.substr(1) vs word.splice(1) ?

Collapse
 
kais_blog profile image
Kai

Note, splice is used for modifying arrays. I think you mean slice.

At first glance, there is no difference between word.substr(1) and word.slice(1). Both return a substring of word. However, their signature differs. The second parameter to substr is length. So, using substr(1, 5) means, take 5 characters starting at 1. In contrast, slice(1, 5) means take the characters between index 1 (inclusive) and index 5 (exclusive).

Besides, there's also a substring method. This behaves pretty much like slice. It takes a starting index and an optional ending index.

All three methods are valid ways to extract a substring. They may differ a bit in performance. Yet, the difference is more or less negligible. Choose what you find adequate.

Collapse
 
machineno15 profile image
Tanvir Shaikh

Got it, Thanks Kai it's very helpful