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.
For point 6.
whats the difference between
word.substr(1) vs word.splice(1) ?
Note,
splice
is used for modifying arrays. I think you meanslice
.At first glance, there is no difference between
word.substr(1)
andword.slice(1)
. Both return a substring ofword
. However, their signature differs. The second parameter tosubstr
islength
. So, usingsubstr(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 likeslice
. 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.
Got it, Thanks Kai it's very helpful