Last active
May 18, 2018 18:12
-
-
Save rishabhgupta/91b3eb5d39bb8be39e0613baea2f939f to your computer and use it in GitHub Desktop.
ES6 Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getNum = () => 5; | |
// Translates to | |
var getNum = function(){ | |
return 5; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var modByTwo = val => val % 2; | |
// Translates to | |
var modByTwo = function(val){ | |
return val % 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var add = (val1, val2) => val1 + val2; | |
// Translates to | |
var add = function(val1, val2){ | |
return val1 + val2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addTax = function (price, tax = price * 0.2){ | |
console.log(price + tax); | |
} | |
addTax(100); // OUTPUT: 120 | |
addTax(100, 30); // OUTPUT: 130 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var currencies = ['Bitcoin', 'Ether', 'Ripple']; | |
for (var carrency of currencies){ | |
console.log(carrency); | |
} | |
//OUTPUT: Bitcoin, Ether, Ripple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firstname = 'Cipher'; | |
var lastname = 'Hack'; | |
var obj = { | |
firstname, | |
lastname | |
} | |
console.log(obj) | |
//OUPUT | |
{firstname: 'Cipher', lastname: 'Hack'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dynamicKey = 'dKey'; | |
var value = 'Cipherhack'; | |
var obj = { | |
[dynamicKey + '-01']:value | |
} | |
console.log(obj); | |
// OUPUT: {dKey-01: "Cipherhack"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firstname = 'Cipher'; | |
var lastname = 'Hack'; | |
var obj = { | |
firstname, | |
lastname, | |
log(){ | |
console.log(this.firstname + " " + this.lastname); | |
} | |
} | |
console.log(obj.log()); | |
//OUPUT: Cipher Hack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var octal = 0o10; | |
console.log(octal); | |
// OUTPUT: 8 | |
var binary = 0b10; | |
console.log(binary); | |
// OUTPUT: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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