Javascript: Lexicographic Sorting

In Javascript, what would you expect the following expression to produce?

[1, 10, 8].sort();

If you thought this would evaluate to [1, 8, 10], you would benefit from a better understanding of the way Javascript deals with arrays. The above expression actually evaluates to:

[1, 10, 8].sort() = [1, 10, 8]

Why is this? The answer:

  • Javascript arrays can contain any valid value type, including null, String, Number, Boolean or another Object;
  • therefore, the Array.prototype.sort() method first applies the toString() method on each element of the array, and then sorts them in lexicographic order — also known as alphabetical sorting — as opposed to numerical order.

The Future of JavaScript: ECMAScript 6

http://html5hub.com/10-ecmascript-6-tricks-you-can-perform-right-now/#i.1ovozod1ak3fis

JavaScript was developed by Brendan Eich and released in 1995. A year later it was developed into a standard: ECMAScript, which today is the foundation of not only JavaScript but also ActionScript, JScript and others.

Today’s version of JavaScript is based on ECMAScript 3 [what happened to the other versions?]. The next big thing, JavaScript 2.0, is ECMAScript 6, which will introduce a lot of new functionality, some of which has been part of popular libraries for some time, for example Underscore.js.

ECMAScript 6 is code-named “Harmony” or “ES.next”. It is meant to be a better language for the complex applications and libraries that thousands of developers are using JS for today.

Let’s look at some of these new features, and some in more detail.

Ways to use ECMA6 today, before the standard is finalized:

It appears that, schockingly, “ECMA6” support will vary among browsers.

  • Default function parameters:(specification documentation)
  • var myNameIs = function(firstName = "John", lastName = "Doe") {
      console.log("Hi, my name is " + firstName + " " + lastName);
    }
    
  • Sets
  • Maps
  • ‘Destructuring’ for Arrays and Objects

http://net.tutsplus.com/articles/news/ecmascript-6-today/