//ES5var name5 ="Jane Smith"var age5 =23
name5 ="Jane Miller"
console.log(name5)//Jane Miller//ES6/* constants can not be changed, will get an error
can mutate it though if it is object (with .push() for ex) */const name6 ="Jane Smith"let age6 =23
name6 ="Jane Miller"
console.log(name6)//Uncaught TypeError: invalid assignment to const 'name6'//ES5functiondriverLicense5(passedTest){if(passedTest){var firstname ="John"var yearOfBirth =1990}
console.log(
firstname +" whom was born in "+ yearOfBirth +" passed the test!")}driverLicense5(true)//John whom was born in 1990 passed the test!//ES6functiondriverLicense6(passedTest){if(passedTest){//let and const only accessible in blocklet firstname ="John"const yearOfBirth =1990}
console.log(
firstname +" whom was born in "+ yearOfBirth +" passed the test!")}driverLicense6(true)// Uncaught ReferenceError: firstname is not defined//ES5var i5 =5for(var i5 =2; i5 >0; i5--){
console.log(i5)}
console.log(i5)// 0//ES6let i6 =6for(let i6 =2; i6 >0; i6--){
console.log(i6)}
console.log(i6)// 6
Blocks and IIFEs
//ES5;(function(){var c =3})
console.log(c)//c is not defined//ES6{const a =1let b =2var c =3}
console.log(a)// not accessible
console.log(c)// accessible
Strings
let firstName ="John"let lastName ="Smith"const yearOfBirth =1990functioncalcAge(){return2017- yearOfBirth
}//ES5
console.log("This is "+
firstName +" "+
lastName +" and he is "+calcAge(yearOfBirth)+".")//ES6
console.log(`This is ${firstName}${lastName} and he is ${calcAge(yearOfBirth)}.`)const n =`${firstName}${lastName}`
console.log(n.startsWith("J"))//true
console.log(n.endsWith("h"))//true
console.log(n.endsWith("n"))//false
console.log(n.includes("oh"))//true
console.log(`${firstName}`.repeat(3))//John John John
Arrow Functions
const years =[1990,1965,1982,1937]//ES5var ages5 = years.map(function(e){return2017- e
})
console.log(ages5)//[27, 52, 35, 80]//ES6let ages6 = years.map(e=>2017- e)// no need for return in one-line functions
console.log(ages6)//[27, 52, 35, 80]
ages6 = years.map((e, index)=>`Age element ${index +1}: ${2017- e}.`)
console.log(ages6)
ages6 = years.map((e, index)=>{const now =newDate().getFullYear()const age = now - e
return`Age element ${index +1}: ${age}.`})
console.log(ages6)//[ "Age element 1: 27.", "Age element 2: 52.", "Age element 3: 35.", "Age element 4: 80." ]
Arrow Functions 2
//ES5var box5 ={color:"green",position:"1",clickMe:function(){var self =this
document.querySelector(".green").addEventListener("click",function(){//this points to window object not to box5 thats why we create self variablevar str ="This is box number "+ self.position +" and it is "+ self.color
alert(str)})},}
box5.clickMe()//ES6const box6 ={color:"green",position:"1",clickMe:function(){// if you switch this to es6 it is not gonna work '() =>'
document.querySelector(".green").addEventListener("click",()=>{// es6 you don't need to create a self variablevar str ="This is box number "+this.position +" and it is "+this.color
alert(str)})},}
box6.clickMe()//arrow functions should never be used as methods//function constructorfunctionPerson(name){this.name = name
}//ES5Person.prototype.myFriends5=function(friends){var arr = friends.map(function(e){returnthis.name +" is friends with "+ e
}.bind(this))//if you don't bind it this takes window --bind creates a copy
console.log(arr)}var friends =["Bob","Jane","Mark"]newPerson("John").myFriends5(friends)//ES6Person.prototype.myFriends6=function(friends){var arr = friends.map(e=>`${this.name} is friends with ${e}`)
console.log(arr)}newPerson("Mike").myFriends6(friends)
Destructuring
//ES5var john =["John",26]var name = john[0]var age = john[1]//ES6const[name1, age1]=["John",26]
console.log(name1)// John
console.log(age1)// 26const obj ={firstName1:"John",lastName1:"Smith",}const{ firstName1, lastName1 }= obj
console.log(firstName1)// John
console.log(lastName1)// Smithconst{firstName1: a,lastName1: b }= obj
console.log(a)// John
console.log(b)// SmithfunctioncalcAgeRetirement(year){const age =newDate().getFullYear()- year
return[age,65- age]}const[age, retirement]=calcAgeRetirement(1990)
console.log(age)//age
console.log(retirement)// (65 - age)
Arrays
const boxes = document.querySelectorAll(".box")//ES5var boxesArr5 =Array.prototype.slice.call(boxes)
boxesArr5.forEach(function(cur){
cur.style.backgroundColor ="red"})//ES6const boxesArr6 = Array.from(boxes)
boxesArr6.forEach(cur=>(cur.style.backgroundColor ="red"))//ES5for(var jbox =0; jbox < boxesArr5.length; jbox++){if(boxesArr5[jbox].className ===" box blue"){continue}
boxesArr5[jbox].textContent ="I am changed to blue"}//ES6for(const current of boxesArr6){if(current.className.includes("blue")){continue}
current.textContent ="I am changed to red"}var ages =[12,17,8,21,14,11]var full5 = ages.map(function(cur){return cur >=18})
console.log(full5)// [false, false, false, true, false, false]//ES5
console.log(full5.indexOf(true))// 3
console.log(ages[full5.indexOf(true)])// 21//ES6
console.log(ages.findIndex(cur=> cur >=18))// 3
console.log(ages.find(cur=> cur >=18))// 21
Spread Operator
//used in functionfunctionaddFourAges(a, b, c, d){return a + b + c + d
}var sum1 =addFourAges(18,30,12,21)
console.log(sum1)//81//ES5//if we want to use an array we need apply in es5var ages =[18,30,12,21]var sum2 =addFourAges.apply(null, ages)
console.log(sum2)//81//ES6//but in es6 we use spread (...)const sum3 =addFourAges(...ages)
console.log(sum3)//81const familySmith =["John","Jane","Mike"]const familyDoe =["Nick","Mary"]//ES5var bigFamily = familySmith.concat("Lana").concat(familyDoe)//ES6const bigFamily =[...familySmith,"Lana",...familyDoe]
console.log(bigFamily)//[ "John", "Jane", "Mike", "Lana", "Nick", "Mary" ]const h = document.querySelector("h1")const boxes1 = document.querySelectorAll(".box")//node-listconst all =[h,...boxes1]
Array.from(all).forEach(cur=>(cur.style.color ="orange"))
console.log("<---->")
Rest Parameters
/* if we don't know how many arguments are there gonna be
used in function declaration *///ES5functionisFullAge5(){
console.log(arguments)//similar to this variable//makes arguments an arrayvar argsArr =Array.prototype.slice.call(arguments)
argsArr.forEach(function(cur){
console.log(2016- cur >=18)})}isFullAge5(1990,1999,1965)//ES6functionisFullAge6(...years){
years.forEach(cur=> console.log(2016- cur >=18))}isFullAge6(1990,1999,1965)//ES5functionisFullAge51(limit){//starts cutting from position onevar argsArr1 =Array.prototype.slice.call(arguments,1)
argsArr1.forEach(function(cur){
console.log(2016- cur >= limit)})}isFullAge51(16,1990,1999,1965)//ES6functionisFullAge61(limit,...years){
years.forEach(cur=> console.log(2016- cur >= limit))}isFullAge61(16,1990,1999,1965)
Default Parameters
//ES5//function constructorfunctionSmith5(firstName, yearOfBirth, lastName, nationality){//default lastName and nationality if not specified
lastName ===undefined?(lastName ="Smith"):(lastName = lastName)
nationality ===undefined?(nationality ="American"):(nationality = nationality)this.firstName = firstName
this.lastName = lastName
this.yearOfBirth = yearOfBirth
this.nationality = nationality
}//ES6functionSmith6(
firstName,
yearOfBirth,
lastName ="Smith",
nationality ="American"){//default lastName and nationality if not specifiedthis.firstName = firstName
this.lastName = lastName
this.yearOfBirth = yearOfBirth
this.nationality = nationality
}var john =newSmith6("John",1990)
console.log(john)//{ firstName: "John", lastName: "Smith", yearOfBirth: 1990, nationality: "American" }//last name and nationality changedvar emily =newSmith6("Emily",1980,"Diaz","Spanish")
console.log(emily)// { firstName: "Emily", lastName: "Diaz", yearOfBirth: 1980, nationality: "Spanish" }
Maps
//new key value data structureconst question =newMap()//in order to add data we use set method
question.set("question","What is the capitol?")
question.set(1,"Brussels")
question.set(2,"Washington")
question.set(3,"Ankara")
question.set(4,"Caracas")//correct answer
question.set("correct",2)//if the answer is correct
question.set(true,"Correct")// if it is wrong
question.set(false,"Wrong try again")//get
console.log(question.get("question"))//What is the capitol?//size of map
console.log(question.size)// 8if(question.has(4)){//checks if it has number 4
question.delete(4)//4th answer is gone}//empty the map
question.clear()// question.forEach((value, key) => console.log(`this is ${key}, and it's set to ${value}`));for(let[key, value]of question.entries()){//same as forEach methodif(typeof key ==="number"){//if key is number then print
console.log(`Answer ${key}: ${value}`)}}//convert to numberconst ans =parseInt(prompt("Write the correct answer for capitol"))
console.log(question.get(ans === question.get("correct")))
Classes
//ES5varPerson5=function(name, yearOfBirth, job){this.name = name
this.yearOfBirth = yearOfBirth
this.job = job
}Person5.prototype.calculateAge=function(){var age =newDate().getFullYear -this.yearOfBirth
console.log(age)}var mioke5 =newPerson5("Mioke",1970,"engineer")//ES6classPerson6{constructor(name, yearOfBirth, job){this.name = name
this.yearOfBirth = yearOfBirth
this.job = job
}//no need for ; or ,calculateAge(){//no need for prototypevar age =newDate().getFullYear -this.yearOfBirth
console.log(age)}staticgreeting(){//attached, not inherited
console.log("Hey there I am Person6")}}const mioke6 =newPerson6("Mioke",1970,"engineer")
Person6.greeting()//Static methods won't work at instances
mioke6.greeting()
//request and fetch (fetch is more modern)functiongetWeather(){const url ="some_url"fetch(url).then(result=>{
console.log(result)//converting json to javascriptreturn result.json()}).then(data=>{//prints the json
console.log(data)const today = data.consolidated_weather[0]
console.log(`temperatures in ${data.title} stay between
${today.min_temp} and ${today.max_temp}.`)}).catch(error=>{
console.log(error)})}getWeather(2487956)//fetch with await/asyncasyncfunctiongetWeatherAw(){const url ="some_url"//tries if there is an errortry{const result =awaitfetch(url)const data =await result.json()
console.log(data)return data
}catch(error){alert(error)}}getWeatherAw()let dataCity
getWeatherAw(someNumber).then(data=>{
dataCity = data
console.log(dataCity)})
Promises
//asyncconstsecond=()=>{setTimeout(()=>{
console.log('no there is Async');},2000);//gives two seconds delay};constfirst=()=>{
console.log('First');second();
console.log('the end');};first();//async with callbacks (old way)functiongetRecipe(){setTimeout(()=>{const recipeId =[123,281,234,12];
console.log(recipeId);setTimeout(id=>{const recipe ={title:'Pizza',publisher:'Damon'};
console.log(`${id}: ${recipe.title}`);setTimeout(id=>{const recipe2 ={title:'Pasta',publisher:'John'};
console.log(`${id}: ${recipe2.title}, publisher: ${recipe2.publisher}`);},1500, recipeId[2]);},1000, recipeId[1]);//it ll be 2500 ms; third argument is parameter for the function},1500);};getRecipe();//promises (es6)const getIds =newPromise((resolve, reject)=>{//setTimeout can not be failedsetTimeout(()=>{resolve([123,281,234,12]);},1500);});constgetRecipe=recId=>{returnnewPromise((resolve, reject)=>{setTimeout(id=>{const recipe ={title:'Pasta',publisher:'Damon'};resolve(`${id}: ${recipe.title}`);},1500, recId)});};constgetRelated=publisher=>{returnnewPromise((resolve, reject)=>{setTimeout(pub=>{const recipe ={title:'Pizza',publisher:'John'};resolve(`${pub}: ${recipe.title}`);},1500, publisher);});};//then and catch
getIds
//this is for resolve.then(ids=>{//[123, 281, 234, 12]
console.log(ids);returngetRecipe(ids[2]);}).then(recipe=>{//getRecipe 234:Pasta
console.log(recipe);returngetRelated('John');}).then(recipe=>{//getRelated John: Pizza
console.log(recipe);}).catch(error=>{//this is for reject
console.log(error);});*///async-await/*await can only be used with async function
same thing as then catch on top */asyncfunctiongetrecipesAw(){//first promise getIdsconst ids =await getIds;
console.log(ids);const recipe =awaitgetRecipe(ids[2]);
console.log(recipe);const related =awaitgetRelated('John');
console.log(related);return recipe;};/*const rec = getrecipesAw(); // we'll get pending
console.log(rec);*/getrecipesAw().then(result=>{//but if you use then we ll get the result
console.log(`${result} is the best ever!`);});
A personal blog by Volkan Uyarer. Developer, Lifelong Learner, Tech Enthusiast.