Skip to main content

JavaScript Series (02) — Introducing ES6

1. let & const (Block Scope without Hoisting )

<html><body>Check console logs</body>
<script>

window.onload = () => {
let letVariable = 10;
var varVariable = 10;
console.log(`initial value letVariable = ${letVariable}`)
console.log(`initial value varVariable = ${varVariable}`)
{
let letVariable = 20;
var varVariable = 20;
console.log(` intermediate value letVariable = ${letVariable}`);
console.log(` intermediate value varVariable = ${varVariable}`);
}

letVariable = letVariable + 30;
varVariable = varVariable + 30;
console.log(`final value letVariable =${letVariable}`)
console.log(`final value varVariable = ${varVariable}`)
}
</script></html>
const constVariable = 10;
console.log(`initial value for constVariable = ${constVariable}`);
constVariable = constVariable + 1;
console.log(`final value for constVariable = ${constVariable}`);

2. Arrow functions (Expression Bodies)

var sum = function (a,b){ return a+b;};
let sum = (a,b)=> a+b;
function sum(a,b) { return a+b;}

3. Default parameter

// This is not the best method to sum a variable number of arguments // but it shows the power what defaults dofunction withDefaults(a=0, b=0,c=0){ return a+b+c;}
function withoutDefaults(a,b,c){return a+b+c;}
console.log('withDefaults ' + withDefaults(1,1));
console.log('withoutDefaults ' + withoutDefaults(1,1));

4. Template Literals — Interpolation

const person = { name: 'MPS', age: ‘10’ }
console.log(`${name} is ${age} year old`);

5. Enhanced Object Properties

const name = 'MPS', age = 10;const person = {name, age}
console.log(`${person.name} is ${person.age} years old`)
//This is same as ES5 below.
const person2 = { "name": name, "age": age };

6. Destructuring Assignments

const person = { name: 'MPS', age: ‘10' }
const {name, age} = person;
console.log(`name is ${name} and age is ${age}`);
//Prints name is MPS and age is 10
const person = { name: 'MPS' }
const {name, age='100'} = person;
console.log(`name is ${name} and age is ${age}`);
//Prints name is MPS and age is 100
const person = { name: 'MPS', age: ‘10' }
const {name :userName,age :userAge} = person;
console.log(`userName is ${userName} and userAge is ${userAge}`);

7. Promises

8. keywords for clean Object-Oriented Programming

<html><body>Check console logs</body>
<script>
window.onload
= () => {
class Vehicle { constructor(make) {
this.make = make
}
start() {
console.log(`${this.make} started`);
}
}
class Car extends Vehicle { constructor(make, maxSpeed) {
super(make);
this.maxSpeed = maxSpeed;
}
go(){
super.start();
console.log(`We will drive at ${this.maxSpeed} speed`);
}
}
new Car('toyota', 120).go();
}
</script></html>

9. Modules

/** File DoThings.js **/export doSomething =()=>{console.log(‘I'm doing something);}
export doSomethingElse =()=>{console.log(‘I'm doing something else ’);}
export doRandom =()=>{console.log(‘I'm doing random Thing);}
export doNothing =()=>{console.log(‘I'm doing nohing);}
export const someConst = ’Some Value’;
/** File App.js ****/
import {doSomething,doNothing,someConst} from ‘./DoThings.js'
/*** File Something.js ***/
export default ()=>{console.log(‘I'm doing something);}
/** App.js ****/
import anyName from ’Something'

Conclusion

Comments