New Features

Each version of ES has introduced new functionality. To keep this guide simple - we will only focus on the more major additions.

ES2015 (ES6)

Variable Declarations and Scope

There are new ways to define variables, each definition type has it's own purpose.

There are different types of scope at play in Javascript post ES2015.

Function Scope

This is the same scope system used in PHP, variables inside functions/methods are only accessible inside the function itself, this means you can not access data in other functions, only the parameters passed and class properties are available.

Variables defined with var are function scoped.

Block Scope

This is not used in PHP but is similar to function scope. Block scope expands on function scope by further limiting access to variables inside logical blocks of code.

A block is defined by a pair of curly braces { ... }. So a block would include (but not limited to) the following constructs. * if { ... } * for { ... } * while { ... } * switch { ... }

Variables defined with let and const are block scoped. Block Block scoped variables are accessible in the block they are defined in as well as any nested blocks. A variable defined inside a block is not accessible outside of the block.

It is advised that let is used instead of var where possible to limit scope.

Const and immutability

Once a value has been assigned to a const variable, it can not be re-assigned. This is technically correct but if the variable in question is an object with properties, you can still mutate the properties themselves.

In depth guide on the different types of declarations