Handling Edge Cases and Errors
As you can see, if the starting index is greater than the ending index, substring will swap their positions. If either index is negative, it will be treated as 0.
Comparing Substring with Other String Manipulation Methods
As you can see, all three methods can achieve similar results. However, slice() and substr() behave differently when dealing with negative indices, and substr() takes the length of the substring as its second parameter, rather than the ending index. It’s essential to understand these differences when choosing the right method for your task.