Skip to content

Instantly share code, notes, and snippets.

@rishabhgupta
Last active May 18, 2018 18:12
Show Gist options
  • Save rishabhgupta/91b3eb5d39bb8be39e0613baea2f939f to your computer and use it in GitHub Desktop.
Save rishabhgupta/91b3eb5d39bb8be39e0613baea2f939f to your computer and use it in GitHub Desktop.
ES6 Examples
var getNum = () => 5;
// Translates to
var getNum = function(){
return 5;
}
var modByTwo = val => val % 2;
// Translates to
var modByTwo = function(val){
return val % 2;
}
var add = (val1, val2) => val1 + val2;
// Translates to
var add = function(val1, val2){
return val1 + val2;
}
let cipherhack = 100;
if (cipherhack > 0) {
let cipherhack = 10;
}
console.log(cipherhack);
// OUTPUT: 100
const cipherhack2 = 100;
if (cipherhack2 > 0){
const cipherhack2 = 2;
}
console.log(cipherhack2);
// OUTPUT 100
'use strict'
const CONTROL_GROUP;
// Uncaught SyntaxError: Missing initializer in const declaration
const CONTROL_GROUP = 10;
CONTROL_GROUP = 100;
console.log(CONTROL_GROUP);
// Uncaught TypeError: Assignment to constant variable.
var greetUser = function(greeting = "Hi", userName = "user"){
console.log(greeting + "! " + userName + '.');
}
greetUser(); // OUTPUT: Hi! user.
greetUser('Hello'); // OUTPUT: Hello! user.
greetUser(undefined, 'Cipherhack'); // OUTPUT: Hi! Cipherhack.
greetUser('Hello', 'Cipherhack'); // OUTPUT: Hello! Cipherhack.
var addTax = function (price, tax = price * 0.2){
console.log(price + tax);
}
addTax(100); // OUTPUT: 120
addTax(100, 30); // OUTPUT: 130
var arr = [100, 200, 300];
var [small, mid, large] = arr;
console.log(large)
// OUTPUT: 300
var [small, ...remaining] = arr;
console.log(remaining);
// OUTPUT [200, 300]
var person = {firstname: 'Cipher', lastname:'Hack', address: 'Hello World'};
// variables should match with objects keys
let {firstname, lastname, address} = person
console.log(lastname);
// OUTPUT: "Hack"
// We can also rename the variables
let {firstname:fname, lastname:lname, address:add} = person
console.log(lname);
// OUTPUT: "Hack"
var currencies = ['Bitcoin', 'Ether', 'Ripple'];
for (var carrency of currencies){
console.log(carrency);
}
//OUTPUT: Bitcoin, Ether, Ripple
'use strict';
// using var, cipherhack will be hoisted
console.log(cipherhack);
var cipherhack = 1;
//OUTPUT: undefined
// using let, cipherhack2 will not be hoisted
console.log(cipherhack2);
let cipherhack2 = 2;
//OUTPUT: ReferenceError: cipherhack2 is not defined
//using let and assigning
let cipherhack3 = 3;
console.log(cipherhack3);
//OUTPUT: 3
//using let and not assigning
let cipherhack3;
console.log(cipherhack3);
//OUTPUT: undefined
var firstname = 'Cipher';
var lastname = 'Hack';
var obj = {
firstname,
lastname
}
console.log(obj)
//OUPUT
{firstname: 'Cipher', lastname: 'Hack'}
var dynamicKey = 'dKey';
var value = 'Cipherhack';
var obj = {
[dynamicKey + '-01']:value
}
console.log(obj);
// OUPUT: {dKey-01: "Cipherhack"}
var firstname = 'Cipher';
var lastname = 'Hack';
var obj = {
firstname,
lastname,
log(){
console.log(this.firstname + " " + this.lastname);
}
}
console.log(obj.log());
//OUPUT: Cipher Hack
'use strict'
var octal = 0o10;
console.log(octal);
// OUTPUT: 8
var binary = 0b10;
console.log(binary);
// OUTPUT: 2
function asyncFn() {
let prom = new Promise(function(resolve, reject){
console.log('Entered in promise');
setTimeout(function(){
console.log('resolving promise');
resolve('OK');
},2000);
});
return prom;
}
asyncFn().then(function(data){
console.log(data);
}, function(err) {
console.log(err);
});
// OUTPUT"
// Entered in promise
// Promise {<pending>}
// resolving promise
// OK
var getProduct = function (product, ...companies){
console.log(companies);
console.log(companies instanceOf Array);
}
getProducts('Mobile', "Apple", "Samsung")
// OUTPUT: ["Apple", "Samsung"]
// OUTPUT: true
getProducts('Mobile')
// OUTPUT: []
// OUTPUT: true
var ages = [12,3,43,80];
var oldest = Math.max(...ages);
console.log(oldest);
// OUTPUT: 80
var chars = ["A", ..."BCD", "E"]
console.log(chars);
// OUTPUT: ["A","B","C","D","E"]
'use strict';
var productId = 123;
console.log(`Product ID: ${productId}`);
// OUTPUT: Product ID: 123
console.log(`Product ID: ${"APPLE-" + productId}`); // Expression
// OUTPUT: Product ID: 123
Product ID: APPLE-123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment