JavaScript 1.6

New in JavaScript 1.6

 Array

  • indexOf() – returns the index of the given item’s first occurrence.
  • lastIndexOf() – returns the index of the given item’s last occurrence.

The iterative methods are:

  • every() – runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
  • filter() – runs a function on every item in the array and returns an array of all items for which the function returns true.
  • forEach() – runs a function on every item in the array.
  • map() – runs a function on every item in the array and returns the results in an array.
  • some() – runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit.
1
2
3
4
5
6
function isLetter(character) {
  return (character >= "a" && character <= "z");
}
if (Array.prototype.every.call(str, isLetter))
  alert("The string '" + str + "' contains only letters!");

This notation is rather wasteful and JavaScript 1.6 introduces a generic shorthand:

1
2
if (Array.every(str, isLetter))
  alert("The string '" + str + "' contains only letters!");

Similarly you can easily apply String methods to any object:

1
2
var num = 15;
alert(String.replace(num, /5/, '2'));

 

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s