Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Created June 15, 2023 10:22
Show Gist options
  • Save QuocCao-dev/ed6da1cfa32b2929ea299bb9636b39ad to your computer and use it in GitHub Desktop.
Save QuocCao-dev/ed6da1cfa32b2929ea299bb9636b39ad to your computer and use it in GitHub Desktop.
New Feature

1. Object Literals

Refactor the code:

var name = "Francis";
var lastname = "Jones"
var age = 23;
var obj
function createObject(name,lastname,age){
   obj = {
       name:name,
       lastname:lastname,
       age:age,
   }
   return obj;
}
console.log(createObject(name,lastname,age))

2. Default function arguments

We want to run a function that greets the user by his name, but if the name is not provided we want to show a default name. Old ES5 code:

function greeting(name){
   if(name === undefined){
       name = 'user'
   }
   return 'Hello '+ name;
}
console.log(greeting())

3.Rest and Spread

Refactor the code using ES6 and the rest operator.

function totalDistance(distance1,distance2,distance3){
   var distances = [distance1,distance2,distance3]
   var total = 0;
   for(var i = 0; i < distances.length;i++){
       total += distances[i]
   }
   return total;
}
console.log(totalDistance(200,100,200))

4. Rest and Spread: Exercise two

Refactor the code:

function showItems(arg1,arg2,arg3){
   var arr = [arg2,arg3].concat(arg1)
   console.log(arr)
}
showItems(["dogs","cats"],"turtles","sharks");

5. Refactor code

function getLocation(city,country,continent){
  if(typeof country === 'undefined'){
    country = 'Italy'
  }
  if(typeof continent === 'undefined'){
    continent = 'Europe'
  }
  console.log(continent,country,city)
}

getLocation('Milan')
// Europe Italy Milan

getLocation('Paris','France')
// Europe France Paris

6. Refactor code

function calculatePrice(total, tax = 0.1, tip = 0.05){
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
return total + (total * tax) + (total * tip);
}

7. String Methods

  • indexOf()
  • slice()
  • toUpperCase()
  • toLowerCase()

New Methods:

  • startsWith()
const code = "ABCDEFG";

console.log(code.startsWith("ABB"));
// false
console.log(code.startsWith("abc"));
// false, startsWith is case sensitive
console.log(code.startsWith("ABC"));
// true
  • endsWith()
const code = "ABCDEF";
console.log(code.endsWith("DDD"));
// false
console.log(code.endsWith("def"));
// false, endsWith is case sensitive
console.log(code.endsWith("DEF"));
// true
  • includes()
const code = "ABCDEF"

console.log(code.includes("ABB"));
// false
console.log(code.includes("abc"));
// false, includes is case sensitive
console.log(code.includes("CDE"));
// true
  • repeat()
let hello = "Hi";
console.log(hello.repeat(10));
// "HiHiHiHiHiHiHiHiHiHi"

8. Refactor code

var person  = {
  first: "Alberto",
  last: "Montalesi"
}

var first = person.first;
var last = person.last;
console.log(first,last);
const person = {
  name: "Alberto",
  last: "Montalesi",
  links:{
    social: {
      facebook: "https://www.facebook.com/alberto.montalesi",
    },
    website: "http://albertomontalesi.github.io/"
  }
}

const { facebook } = person.links.social;
console.log(facebook);

9. Iterables and Looping

Run code below

var fruits = ['apple','banana','orange'];
for (var i = 0; i < fruits.length; i++){
  console.log(fruits[i]);
}
// apple
// banana
// orange
const fruits = ['apple','banana','orange'];
for(const fruit of fruits){
  console.log(fruit);
}
// apple
// banana
// orange

10. Iterables and Looping: Object

  • Object.keys()
  • Object.entries()
const car = {
  maker: "BMW",
  color: "red",
  year : "2010",
}

for (const prop of Object.keys(car)){
  const value = car[prop];
  console.log(prop,value);
}

11 Difference between for of and for in

let list = [4, 5, 6];

// for...in returns a list of keys
for (let i in list) {
   console.log(i); // "0", "1", "2",
}


// for ...of returns the values 
for (let i of list) {
   console.log(i); // "4", "5", "6"
}

12. Refactor code

const name = "Alberto";
const surname = "Montalesi";
const age = 25;
const nationality = "Italian";

const person = {
  name: name,
  surname: surname,
  age: age,
  nationality: nationality,
}

console.log(person);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment