A guide to EcmaScript 5

For those still supporting old versions of Internet Explorer

By Ken Doman

What is EcmaScript?

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.

How are EcmaScript and JavaScript related?

EcmaScript is the guide that says "this is how JavaScript should function". JavaScript is the language that (mostly) implements EcmaScript.

What do you get with EcmaScript 5?*

  • Array Methods
  • Object methods
  • String.prototype.trim()
  • Function.prototype.bind()
  • JSON methods
  • Strict Mode
  • Date methods

* Not an exhaustive list. See http://kangax.github.io/compat-table/es5/

EcmaScript 5 Array methods

  • Array.prototype.indexOf
  • Array.prototype.lastIndexOf
  • Array.prototype.every
  • Array.prototype.some
  • Array.prototype.forEach
  • Array.prototype.map
  • Array.prototype.filter
  • Array.prototype.reduce
  • Array.prototype.reduceRight

Array.prototype.indexOf

Find first element in array that matches item. Returns position (zero-based)

var fridge = ['ham', 'eggs', 'milk', 'beer'];

console.log(fridge.indexOf('eggs')); // answer => 1

Shows position of first item only.

var fridge = ['eggs', 'milk', 'eggs', 'ham', 'eggs'];

console.log(fridge.indexOf('eggs')); // answer => 0

Returns -1 if no value found.

var fridge = ['ham', 'milk', 'beer'];

console.log(fridge.indexOf('eggs')); // answer => -1

Array.prototype.lastIndexOf

Finds last element in array that matches item. Returns position (zero-based)

var fridge = ['ham', 'eggs', 'milk', 'beer'];

console.log(fridge.indexOf('eggs')); // answer => 1

Shows position of last item only.

var fridge = ['eggs', 'milk', 'eggs', 'ham', 'eggs'];

console.log(fridge.indexOf('eggs')); // answer => 4

Returns -1 if no value found.

var fridge = ['ham', 'milk', 'beer'];

console.log(fridge.indexOf('eggs')); // answer => -1

Array.prototype.every

Uses test function to determine if every value returns true. Stops after first false statement. Returns true if all values are true, or false if one is false.

var isFresh = function (egg) { return egg === "fresh"; };

var eggCarton = ['fresh', 'fresh', 'fresh', 'fresh'];

var allFresh = eggCarton.every(isFresh);

console.log(allFresh) // answer => true;

var eggCarton = ['fresh', 'rotten', 'fresh', 'fresh'];

var allFresh = eggCarton.every(isFresh);

console.log(allFresh) // answer => false;

Array.prototype.some

Uses test function to determine if some value returns true. Stops after first true statement. Returns true if at least one value tests true, or false if all test false.

var isRotten= function (egg) { return egg === "rotten"; };

var eggCarton = ['fresh', 'fresh', 'fresh', 'fresh'];

var someRotten= eggCarton.some(isRotten);

console.log(someRotten) // answer => false;

var eggCarton = ['fresh', 'rotten', 'fresh', 'fresh'];

var someRotten= eggCarton.some(isRotten);

console.log(someRotten) // answer => true;

Array.prototype.forEach

Runs a function on each value in an array. Doesn't care what happens to them afterward.

 

Array.prototype.map

Runs a function on each value in an array, and returns something from it.

var getEggWhites = function (egg) { return egg.white; };

// ... assume we have a a carton of eggs

var eggWhites = eggCarton.map(getEggWhites);

A

By Ken Doman

A

  • 463
Loading comments...

More from Ken Doman