Created
June 20, 2018 13:13
-
-
Save DamienDoumer/07e94dcd8304b75142c4e8779f18a208 to your computer and use it in GitHub Desktop.
Brief Javascript ES6 Tutorial For Beginners Part I All Code Gist
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
## Replace var | |
``` | |
//declare a variable which can be reassigned to another value | |
let name = 'my name'; | |
//defines a variable whose value is not reassignable | |
const PI = 3.14; | |
``` | |
## No more stress with complex strings | |
``` | |
let student = {name : 'Harry', age : '18'}; | |
//See how representing it is easier with `` | |
let description = `I'm a student my name is ${student.name} | |
I'm ${student.age} years old.`; | |
console.log(description); | |
``` | |
## Destructuring | |
``` | |
//Extracting values from an array. | |
const axis = [10, 25, -34]; | |
const [x, y, z] = axis; | |
console.log(x, y, z); | |
//Extracting values from an object. | |
const man = { | |
name: 'Peter', | |
age: '30', | |
height: 185 | |
}; | |
const {name, age, height} = man; | |
console.log(name, age, height); | |
``` | |
## Object Literal | |
``` | |
name = 'Peter'; | |
age = '30'; | |
height = 185; | |
const man = { | |
name, | |
age, | |
height | |
}; | |
///Creates a man object and automatically set its name, age and height properties | |
console.log(man); | |
//Prints : {name: "Peter", age: "30", height: 185} | |
``` | |
## The Cool For Of Loop | |
``` | |
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
for(let number of numbers) | |
{ | |
console.log(number); | |
} | |
``` | |
## Spread it | |
``` | |
const weapons = ["gun", "sword", "spirit bomb"]; | |
console.log(...weapons); | |
``` | |
## Get The Rest of it | |
``` | |
const weapons = ["gun", "sword","catana", "spirit bomb", "shuriken"]; | |
const [weapon1, weapon2, weapon3, ...otherWeapons] = weapons; | |
console.log(weapon1, weapon2, weapon3, otherWeapons); | |
function sum(...nums) { | |
let total = 0; | |
for(const num of nums) { | |
total += num; | |
} | |
return total; | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment