ES 6: A New Hope

Recursion Optimization: Improving Space Complexity

Proper Tail Calls (PTC)

function foo(n){ 
return 'hello' //this is a tail position but not a tail call

return bar() //this is a tail call, since it is not using anything
//else in the function, the stack can be deleted
}
function foo(n){ 
return 1 + bar()
//close, but not a PTC because it must get the
//value of calling bar and then add one and then return
return foo(n) - foo(n)
// not a PTC because the stack must be maintained to fulfill the
// statement
}

LET and CONST

console.log(a) // Error if(something){ 
let a = 4
console.log(a) // 4
}
console.log(a) // Error
  1. The use of var would have hoisted the variable declaration to the top of the scope (in this case window), and would have defined it where it was defined in the code
  • Let treats each block as a new scope (that includes for loops, if statements, or even ES6's new floating block/Lambda
  • You can now freely reuse i in for loops without worry

Caveats

  • You cannot declare the same variable twice:
var a = 3 
var a = 4
console.log(a) //4
let a = 3
let a = 4 //error
var a = 3
let a = 4 //error
  • While it generally won’t matter to us, there is a Temporal Dead Zone when declaring a variable using let. This means that it actually is secretly hoisted, but it is totally inaccessible, so for 99% of you reading this, you can skip over this bit of information

REST/SPLAT/Arguments statement

 
var args = Array.prototype.slice.call(arguments)
// now I could call any array method on args.
var baz = function(a, b, ...rest){ 
console.log(a)
console.log(b)
console.log(rest)
}
baz(5, 6, 10, 11, 12)
//5
//6
//[10,11,12]

Caveats

  • Rest arguments must be the very last parameter
  • the actual name of the rest argument can be named anything, as long as it is preceded with 3 periods

Spread Operators

var nums = [1,2,3] 
console.log(nums) //[1,2,3]
console.log(...nums) // 1,2,3
function getNums(){ 
return [1,2,3]
}
var b = [0, ...getNums()]; console.log(b) // [0,1,2,3]
function returnTwo(a, b) { 
return [b,a]
}
var a =[1,2,3,4,5] var b = returnTwo(a[0], a[1]) //[2,1] – old way var b = returnTwo(...a); // [2,1] – new way

Destructuring

var {city, state, zip} = {city: "San Francisco", state:"CA", zip: 94133} console.log(city) // San Francisco 
console.log(state) // CA
console.log(zip) // 94133
var {city: c, state: s, zip: z} = {city: "San Francisco",state:"CA", zip: 94133} console.log(c) // San Francisco 
console.log(s) // CA
console.log(z) // 94133
console.log(city) //undefined
var {?city, state, ?zip} = {state: 'CA'}
var ?{city, state, zip} = {nothing: "here"}

Arrow Functions

var fn1 = function(){
return 2
} // old way

var fn2 = () => 2 // new way
var nums =[1,2,3]; 
var res = nums.map(function(n){return n*n})
console.log(res) // [1, 4, 9]
var nums = [1,2,3] 
var res = nums.map(n => n*n);
console.log(res); //[1,4,9]
Car.prototype.drive = function(){ 
this.move()
setTimeout(this.turnLeft, function(){
console.log(this) //will log Window
})
}
Car.prototype.drive = function(){ 
this.move()
setTimeout(this.turnLeft, () => console.log(this)) //will log Car }

--

--

--

Software Architect

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Quest Henkart

Quest Henkart

Software Architect

More from Medium

Positive Reflection of My Amal Academy Journey

Priceless Humor of Our Times

“To love me is to hold me when I am raining.

So Hard