Introduction, test cases and web browser compatibility tables for JavaScript 1.6, JavaScript 1.7 & JavaScript 1.8
How JavaScript has evolved
Web development life for most of us is based on JavaScript 1.5, or corresponding support in JScript for IE and ECMAScript 3 (more info can be found in John Resig’s Versions of JavaScript). But what’s beyond that? Mozilla, especially due to having JavaScript creator Brendan Eich on board, have been pushing JavaScript feature support forward.
I have looked at the three latest issued releases of JavaScript: 1.6, 1.7 and 1.8, tested the code in various web browsers and evaluated the results.
The JavaScript tests and compatibility tables
For this, I’ve created a mini-web site for JavaScript testing and web browser compatibility, which offers you:
- JavaScript tests which runs immediately in your web browser
- Web browser compatibility tables
- Code examples, together with expected results (when tests were successful)
It currently contains three sub-sections for each version of JavaScript:
It should also be noted, for anyone looking for support in Internet Explorer, that neither IE 6, IE 7 or IE 8 supports anything of this…
Below I will list the new features for each JavaScript language release of these three, and code examples. All of this code, runnable, is also available in the test site, but listed here for convenience for anyone who wants to get a overview of the new features.
JavaScript 1.6
- Array extras
- indexOf
- lastIndexOf
- every
- filter
- forEach
- map
- some
- Array and String generics
- Array generics
- String generics
(For complete information and documentation, read New in JavaScript 1.6.)
Array extras
New features to enhance the usability of array objects.
The indexOf methodReturns the index of the first occurrence of the item.
var arr = ["Microsoft", "Mozilla", "Apple"];The lastIndexOf method
arr.indexOf("Mozilla");
Returns the index of the last occurrence of the item.
var arr = ["Firefox", "<acronym title="Internet Explorer">IE</acronym>", "Chrome", "Firefox"];The every method
arr.lastIndexOf("Firefox");
Runs a function on each item in the Array, while it returns true.
var arr = [4, 7, 10];
arr.every(function (value) {
return value > 5;
});
var arr = [6, 7, 10];The filter method
arr.every(function (value) {
return value > 5;
});
Runs a function to filter an Array, and returns all items where the function returned true.
var arr = [4, 7, 10];The forEach method
arr.filter(function (value) {
return value > 5;
});
Runs a function on each item in an array.
var arr = ["Firefox", "<acronym title="Internet Explorer">IE</acronym>", "Chrome", "Opera", "Safari"]The map method
resultValues = [];
arr.forEach(function (value) {
resultValues.push(value.toUpperCase());
});
Runs a function on each item, and returns the results in an array.
var arr = [4, 7, 10];The some method
arr.map(function (value) {
return value + 5;
});
Runs a function on each item in the Array, while it returns false.
var arr = [4, 7, 10];
arr.some(function (value) {
return value > 5;
});
var arr = [1, 2, 4];
arr.some(function (value) {
return value > 5;
});
Array and String generics
Using methods from other object types to enhance the existing one. I.e. use String methods on Array objects, and Array methods on String objects.
Array genericsvar words = "These are just words";String generics
Array.filter(words, function (value) {
return value.indexOf("s") === -1;
});
var arr = ["Firefox", "Safari", "Opera"];
String.replace(arr, /[aoueiy]/g, "");
JavaScript 1.7
- Generators and Iterators
- Generators
- Iterators
- Array comprehensions
- Using let for block scope
- let statement
- let expression
- let definition
- Destructuring assignment
- Destructuring assignment with let statement
(For complete information and documentation, read New in JavaScript 1.7.)
Generators and Iterators
Helper features for iterating over items.
GeneratorsCreating generator iterators with the yield keyword, to have state variables.
function double5() {
var i=0, j=5, k;
while (true) {
yield i;
k = i;
i = j;
j += i;
}
}
var g = double5(),Iterators
resultValues = [];
for (var i=0; i<5; i++) {
resultValues.push(g.next());
}
A special object making it easier to iterate over a certain amount of informatíon.
var lostInfo = {
name : "Lost",
location : "Island",
numbers : "4 8 15 16 23 42"
};var iteratorObj = Iterator(lostInfo),
resultValues = [],
next;
try{
while (true) {
next = iteratorObj.next();
resultValues.push(next[0] + ": " + next[1]);
}
}
catch (err if err instanceof StopIteration) {
resultValues.push("END OF LIST");
}
catch (err) {
resultValues.push("UNKNOWN ERROR");
}
var lostInfo = {
name : "Lost",
location : "Island",
numbers : "4 8 15 16 23 42"
};
// Notice true below - will return only parameter names and not values, as opposed to the example above
var iteratorObj = Iterator(lostInfo, true),
resultValues = [];
try{
while (true) {
resultValues.push(iteratorObj.next());
}
}
catch (err if err instanceof StopIteration) {
resultValues.push("END OF LIST");
}
catch (err) {
resultValues.push("UNKNOWN ERROR");
}
Array comprehensions
Powerful initialization of arrays.
function setVal (start, end) {
for (let i=start; i<end; ++i) {
yield i;
}
}
var evenValues = [i for each (i in setVal(0, 15)) if (i % 2 == 0)];
Using let for block scope
Connecting certain variables with statements or blocks, effectively creating block scope variables.
let statementvar x = 5,let expression
y = 7,
resultValues = ["Before let statement: " + x + ", " + y];
let (x=15, y=12) {
resultValues.push("In let statement: " + x + ", " + y);
}
resultValues.push("After let statement: " + x + ", " + y);
var x = 1,let definition
y = 3,
resultValues = ["Before let expression: " + x + ", " + y];
let (x=7, y=8) resultValues.push("In let expression: " + x + ", " + y);
resultValues.push("After let expression: " + x + ", " + y);
var letDefinitionResults =
document.getElementById("results-let-definition"),
item;
for (var i=0; i<5; i++) {
item = document.createElement("div");
item.innerHTML = "Click me - I will say " + i;
let j = i;
item.onclick = function () {
alert("I am " + j);
};
letDefinitionResults.appendChild(item);
}
Destructuring assignment
Allows you to get data from arrays, and swap values between various array objects.
var a = 1,
b = 2;
// Value swapping
[a, b] = [b, a];
var a = 1,
b = 2,
c = 3,
d = 4;
// Value swapping
[a, b, c, d] = [d, c, b, a];
var a = 1,
b = 2;
function newValues () {
return [10, 20];
}
// Assigning new values
[a, b] = newValues();
var a = 1,
b = 2;
function newValues () {
return [10, 20, 30];
}
// Assigning new values, ignoring the second one
[a, , b] = newValues();
Destructuring assignment with let statement
Destructirung assignments can get even more powerful in conjunction with let statements.
var griffins = {
father : "Peter",
mother : "Lois",
son : "Chris",
daugher : "Meg",
baby : "Stewie",
dog : "Brian"
},
resultValues = [];
for (let [type, name] in Iterator(griffins)) {
resultValues.push(type + ": " + name);
}
JavaScript 1.8
- Expression closures
- Generator expressions
- Array extras
- reduce
- reduceRight
(For complete information and documentation, read New in JavaScript 1.8.)
Expression closures
Basically, just a shorthand notation for functions.
// Notice the omission of braces and return statementfunction expressionClosure () "Yeah, ok"
Generator expressions
Using generator expressions to complement generators introduced in JavaScript 1.7.
var animals = {
dog : "Nice",
cat : "Independent",
horse : "big"
},
resultValues = [],
iterating = (i + 3 for (i in animals));
try {
while (true) {
resultValues.push(iterating.next());
}
}
catch (err if err instanceof StopIteration) {
resultValues.push("END OF LIST");
}
Array extras
reduceRuns on each item in the array, collecting the results from the previous iteration.
var numbers = [1, 2, 3, 4, 5],reduceRight
returnValues = [];
numbers.reduce(function (prev, current, index, array) {
returnValues.push("Previous: " + prev + ", current: " + current + ", index: " + index);
return current;
});
Runs on each item in the array, collecting the results from the previous iteration, but in reverse order.
var numbers = [1, 2, 3, 4, 5],
returnValues = [];
numbers.reduce(function (prev, current, index, array) {
returnValues.push("Previous: " + prev + ", current: " + current + ", index: " + index);
return current;
});
My own takes
Personally, I’m very excited by new features being introduced to JavaScript, and especially some of them (like the let statement) will change and simplify a lot of code. It’s good to see a number of the other web browsers vendors being aboard with at least some of the features, although (as usual) it’s disheartening to see that Internet Explorer has effectively not gotten any of these features…
Robert has been working with web developing, mostly interface coding, since 1998. His biggest interests lies in HTML, CSS and JavaScript, where especially JavaScript has been a love for quite some time. He regularly blogs at http://www.robertnyman.com about web developing, and is running/partaking in a number of open source projects (http://code.google.com/u/robnyman/). Robert is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone.
- Login or register to post comments
- 2387 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









