Monday, August 31, 2015

Applying Arrays Methods to ECMAScript 5 Strings

In ECMAScript 5, strings behave like arrays, so you can invoke array methods on strings.

Example: brakets operator as alternative to charAt() method

If you want to invoke the array methods on strings you have to make an indirect invocation by the Function.call() method.

Example: invoking the join() and filter() methods on a string

Array methods that modify arrays in-place, like reverse(), splice(), etc, do not work on strings because strings are immutable. So you can convert the string into an array and then use these methods which modify array.

Example: invoking the reverse() method on a string

No comments :

Post a Comment