Substrings in javascript – the IE compatible way

Here’s something that might cause some web developers bang their heads against walls. Internet Explorer behaves incorrectly when substr() is called with a negative start value. It simply returns the whole string instead of N characters from the end.

So instead of calling substr() for the N last characters of a string like this:

var lastPart = myString.substr(-10);

You might want to call it like this:

var lastPart = myString.substr(myString.length - 10);

This way it works the same in all browsers.