ES6
Tags: javascript
Published: 2019-10-27 21:00:25

---
title: ES6
published: true
description: This is a post to describe JavaScript ES6 concepts using practical examples via Emojis.
tags: javascript
---

## Course Credits

- FreeCodeCamp
- Dylan Israel
- Course Link: [View](https://www.youtube.com/watch?v=nZ1DMMsyVyI).

## TOC

- Template Literals
- Destructuring
- Object Literal
- For of loop
- Spread operator
- Rest operator
- Arrow Functions
- Default Params
- Array.includes
- Let & const
- Export & import
- String.padStart(), String.padEnd()
- Classes
- Trailing commas
- Async/Await
- Sets

## Template Literals

```javascript
const fruit = "🍌";
const milk = "🥛";
const bananaShake = (fruit, milk) => `(${fruit}) Banana (${milk}) Milk shake is ready.`;
console.log(`${bananaShake(fruit, milk)}`);
```

## Destructuring

### Objects

```javascript
const purchasedItems = {
  fruit: "🍇",
  vegetable: "🥕",
  bakery: "🍪"
};
const { bakery } = purchasedItems;
console.log(`Hungry? How about try a ${bakery}?`);
```

### Arrays

```javascript
const cart = ["🍇", "🥕", "🍪", "🥩"];
const [item] = cart;
console.log(`Cashier scanned ${item} first.`);
```

## Object Literal

```javascript
const boilEgg = (egg, water, salt) => {
  const recipe = {
    egg,
    water,
    salt
  };
  console.log(`Get a saucepan, Add 3 cups ${recipe.water} and a pinch of ${recipe.salt}. Throw ${recipe.egg} and boil for 10 minutes.`);
}
boilEgg("🥚", "🚰", "🧂");
```

## For of loop

```javascript
let bill = 0;
const receipt = [1.99, 13.67, 14.21, 5.27];
for(const item of receipt) {
  bill += item
};
console.log(`Your total bill is $${bill}.`);
```

## Spread operator

```javascript
const purchasedItems = {
  fruit: "🍇",
  vegetable: "🥕",
  bakery: "🍪"
};
const juiceRecipe = {
  ...purchasedItems,
  kitchen: "sugar",
  device: "blender"
};
console.log(`Trying a ${juiceRecipe.vegetable} juice.`);
```

## Rest operator

```javascript
const cart = [];
const addItem = (...items) => console.log(`${items} added in the card. Total items in the cart: ${items.length}.`);
addItem("🍎","🥭","🍍","🍌");
```

## Arrow Functions

```javascript
const shoppingBag = [
  {id: 1, name: "🥭", type: "fruit"},
  {id: 2, name: "🍎", type: "fruit"},
  {id: 3, name: "🍌", type: "fruit"},
  {id: 4, name: "🥕", type: "vegetable"}
];
const findVegetable = shoppingBag.filter(item => item.type === "vegetable");
findVegetable ? console.log(`${findVegetable.length} vegetable found in the bag.`) : console.log("Forgot to buy vegetables.");
```

## Default Params

```javascript
const readyToCheckout = (limit = 10) => limit <= 10 ? console.log(`Today, shopping was in budget.`) : console.log(`Today, lot of items were bought.`)
readyToCheckout(23);
```

## Array.includes

```javascript
const receipt = ["🍎","🥭","🍍","🍌"];
receipt.includes("🍌") ? console.log(`You bought bananas?`) : console.log(`How come there are no bananas?`);
```

## Let & const

```javascript
let apple;
apple = "🍏";
console.log(`Green Apple: ${apple}`);
apple = "🍎";
console.log(`Red Apple: ${apple}`);
const banana = "🍌";
console.log(`Banana: ${banana}`);
banana = "🌯"; // WRONG
```

## Export & import

- Example: [View source](https://codesandbox.io/s/practical-water-mnzjj).

## String.padStart(), String.padEnd()

```javascript
let apple = "🍏";
let banana = "🍌";
const dozenApples = apple.padStart(12, apple);
const dozenBananas = banana.padEnd(12, banana);
console.log(`Buy dozen apples: ${dozenApples}. Total apples: ${dozenApples.length}`);
console.log(`Buy dozen bananas: ${dozenBananas}. Total bananas: ${dozenApples.length}`);
```

## Classes

- Example: [View source](https://codesandbox.io/s/naughty-frost-lrj7t).

## Trailing commas

Trailing commas after function arguments, object declaration works, is not recommended.

## Async/Await

- Example: [View source](https://codesandbox.io/s/cool-tdd-m7pw1).

## Sets

```javascript
let cart = new Set(["🍇", "🥕", "🍪", "🍪", "🍪", "🍪", "🥩"]);
console.log(`Total unique items in the cart: ${cart.size}`);
```