"I’ve been a member of ECMA TC39 off and on for a decade and a half, and based on what I have seen, JavaScript will catch up with Underscore in 30 to 50 years."
Libraries, Polyfills, Shims as stepping stones.
console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); // true
function x(condition) {
if (condition) {
console.log(value1); // Undefined!
var value1 = "aditya";
}
console.log(value1); // aditya
//ES6
if (condition) {
console.log(value2); // ReferenceError!
let value2 = "punjani";
}
console.log(value2); //Will also be ReferenceError!
}
x(true);
function x(value, time, callback) {
time = time || 10;
callback = callback || function() {};
//Or do some ugly hack with Arguments special object;
}
ES6
function x(value, time=10, callback=function(){}) {
//Do magic here
}
//Rest
function sum(first, ...numbers) {
let result = first,
i = 0,
len = numbers.length;
while (i < len) {
result += numbers[i];
i++;
}
return result;
}
//Spread
let values = [25, 50, 75, 100]
// equivalent to
// console.log(Math.max(25, 50, 75, 100));
console.log(Math.max(...values)); // 100
1. Lexical 'this' binding,
2. Not newable,
3. Cant change 'this'
4. No arguments object.
var reflect = value => value; // is the same as var reflect = function(value) { return value; };
//Lets you iternate over iterable objects - Arrays, Arguments, Nodelists,Generators.
function printArgs() {
for (let arg of arguments) {
console.log(arg);
}
}
// Iterate over all <div> elements
var divs = document.getElementsByTagName("div");
for (let div of divs) {
div.innerHTML = "Changed...";
}
function* gen() { yield 1; yield 2; }
for (let i of gen()) console.log(i); // prints 1 and 2
var items = new Set(); items.add(5); items.add("5"); console.log(items.size()); // 2 console.log(items.has(5)); // true items.delete(5)
console.log(items.has(5)); // false
var p = new Promise(function(resolve,reject){
setTimeout(function(){
resolve("Yay!");
},100);
});
p.then(function(msg){
console.log(msg); // Yay!
});