Saturday, June 23, 2007

Behave yourself, substring.

str.substring(start, end)

According to the AS documentation, if end is -1, it's assumed to be 0. So what does it mean when you have "abcdefg".substring(2, 0)? It means that it will returning the substring starting at position and going backward excluding the last one. So you get "ab" in this case.

What happen if you have str.substring(2, undefined) or str.substring(2, undefined - 1); ? Surprisingly, the behavior is different in either case.
undefined - 1 will give you an NaN. In the first case, passing undefined behaves the same as not passing anything for end. Passing NaN however, makes it behave as if you are passing in a negative number or 0.

Why should I know all this, you ask? Well, if you are using substring in a loop, with the end being a variable, then you must be aware of what will happen if that variable becomes undefined. Of course, the best thing in most cases is to guard your code against that.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home