Last active
September 14, 2024 10:05
-
-
Save mburakerman/cd85592be39c28fd0dfc1cfdff0d5ba4 to your computer and use it in GitHub Desktop.
Es6 In a Nutshell
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
// 1:)let | |
var name = "burak"; | |
console.log(name); | |
//or use let | |
let name2 = "burak2"; | |
console.log(name2); | |
// Why we would use let instead of var? | |
/* In js, there is global and local scope but js doesn't really have a | |
block scoping. For ex; */ | |
var animal = "cat"; | |
if (true) { | |
//var animal="bird"; | |
let name = "bird"; | |
} | |
console.log(animal); //if var is used, output is bird. | |
/* But if we use let, the output will be cat.Cos, the variable has block | |
scoping inside the curl braces */ | |
// Pro let tip; | |
function sayName() { | |
age = 22; | |
} | |
let age; | |
sayName(); | |
console.log(age); | |
//This works.Cos we declared let variable before calling the sayName() function | |
// 2:)const | |
const COLOR = "black"; | |
console.log(COLOR); | |
/* | |
This variable is constant. It may not be changed.For ex if i wanna reassign | |
the variable, i get error. | |
Constants can be declared uppercase or lovercase but mostly used uppercase. | |
*/ | |
const AGES = [1, 2, 30]; | |
console.log(AGES); | |
// But we can push new value to an array! | |
// 3:)functions | |
function lol2() { | |
console.log("I called 2."); | |
} | |
lol2(); | |
//In es6 we can write it like that; | |
var lol3 = () => { | |
console.log("I called 3."); | |
}; | |
lol3(); | |
setTimeout(() => { | |
console.log("I am setTimeout output."); | |
}, 4000); | |
// 4:)Template Literals | |
let myname = "Burak" | |
let xx = "This is a string."; | |
// That is back tick => ` | |
let xx2 = ` | |
first line | |
Multi line string! | |
`; | |
console.log(xx2); | |
// Also we can use variable inside it; | |
let xx3 = ` | |
my name is ${myname} | |
`; | |
console.log(xx2); | |
// 5-)Extract values from array easily | |
let numbers666 = [1, 2, 3]; | |
let [a, b] = numbers666; | |
console.log(a); //output is 1 | |
console.log(numbers666); //output is [1,2,3] | |
//if we want to get part of an array, we can do like this; | |
let [x, ...y] = numbers666; | |
console.log(y) //output is [2,3] | |
// and we can add the numbers666 array values, | |
function add(a, n, c) { | |
console.log(a + n + c); | |
} | |
add(...numbers666); // output is 6 | |
// we can also merge 2 arrays together easily | |
var fruits = ["banana", "kiwi"]; | |
var sports = ["football", "tennis", "basketball"]; | |
// if we want to put fruits array after football, we can just do like that; | |
var sports = ["football", ...fruits, "tennis", "basketball"]; // thats it! | |
// 6-) Classes | |
class Person { | |
constructor(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
displayAge() { | |
console.log(this.age) | |
} | |
} | |
let burak = new Person('Burak', 22); | |
burak.displayAge(); | |
// Inheritance | |
// We grabbed all the codes of Person class and also we can add new attributes here; | |
class Programmer extends Person { | |
//language added additionally | |
constructor(name, age, language) { | |
super(name, age) // we already have that attributes | |
this.language = language; | |
} | |
displayLanguage() { | |
console.log(this.language); | |
} | |
} | |
let mehmet = new Programmer("Mehmet", 23, "JavaScript"); | |
mehmet.displayAge(); | |
mehmet.displayLanguage(); | |
// 7-) Array Iteration | |
var arr = ['a', 'b', 'c']; | |
for(let i of arr) { | |
console.log(i) | |
} | |
// *** === More === *** // | |
// 1-) Computed property names | |
// We can not do this in ES5 | |
var obj = {}; | |
obj[prop] = 'some value'; | |
// If we want, we have to do like this! | |
var obj = { | |
[prop]: 'some value' | |
}; | |
// 2-) Default parameters | |
// This syntax is often used to give function parameters default values. Even when you do know this syntax, it fails spectacularly when dealing with booleans, numbers or other falsy values. | |
function hello(name) { | |
name = name || 'Anonymous'; | |
console.log('Hi ' + name); | |
} | |
//In ES6, we have default parameters also: | |
function hello(name = 'Anonymous') { | |
console.log('Hi ' + name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 130 should be console.log(this.language) instead of console.log(language)