ECMA Script 6 Introduction of topics | Lecture 1



ECMA Script 6 Introduction of topics

1. Let and Const

  • let: Block-scoped variable.
  • const: Block-scoped constant.

let x = 10;

x = 20; // Allowed

 

const y = 30;

// y = 40; // Error: Assignment to constant variable

 

2. Arrow Functions

Concise syntax for defining functions.

// Regular function

function add(a, b) {

  return a + b;

}

 

// Arrow function

const add = (a, b) => a + b;

 

console.log(add(5, 3)); // Output: 8

 

3. Template Literals

For string interpolation and multiline strings.

const name = "John";

const greeting = `Hello, ${name}!`;

console.log(greeting); // Output: Hello, John!

 

const multiline = `This is

a multiline

string.`;

console.log(multiline);

 

4. Default Parameters

Default values for function parameters.

function greet(name = "Guest") {

  return `Hello, ${name}!`;

}

 

console.log(greet()); // Output: Hello, Guest!

console.log(greet("Alice")); // Output: Hello, Alice!

 

5. Destructuring Assignment

Extract values from arrays or objects.

// Array destructuring

const [a, b] = [10, 20];

console.log(a, b); // Output: 10 20

 

// Object destructuring

const person = { name: "Alice", age: 25 };

const { name, age } = person;

console.log(name, age); // Output: Alice 25

 

6. Spread and Rest Operators

  • Spread (...): Expands elements.
  • Rest (...): Collects elements.

// Spread operator

const arr1 = [1, 2, 3];

const arr2 = [...arr1, 4, 5];

console.log(arr2); // Output: [1, 2, 3, 4, 5]

 

// Rest operator

function sum(...nums) {

  return nums.reduce((acc, num) => acc + num, 0);

}

console.log(sum(1, 2, 3)); // Output: 6

 

7. Modules

Export and import functionality between files.

// File: math.js

export const add = (a, b) => a + b;

 

// File: main.js

import { add } from './math.js';

console.log(add(5, 3)); // Output: 8

 

8. Classes

Simpler syntax for object-oriented programming.

class Person {

  constructor(name, age) {

    this.name = name;

    this.age = age;

  }

  greet() {

    return `Hello, my name is ${this.name}.`;

  }

}

 

const person = new Person("John", 30);

console.log(person.greet()); // Output: Hello, my name is John.

 

9. Promises

For handling asynchronous operations.

const fetchData = () => {

  return new Promise((resolve, reject) => {

    setTimeout(() => resolve("Data fetched!"), 2000);

  });

};

 

fetchData().then((data) => console.log(data)); // Output: Data fetched! (after 2 seconds)

 

10. Map and Set

New data structures.

// Map

const map = new Map();

map.set("key1", "value1");

console.log(map.get("key1")); // Output: value1

 

// Set

const set = new Set();

set.add(1);

set.add(2);

set.add(1); // Duplicate ignored

console.log(set); // Output: Set { 1, 2 }

 

11. For-of Loop

Iterates over iterable objects.

const arr = [10, 20, 30];

for (const num of arr) {

  console.log(num);

}

// Output: 10, 20, 30

 

12. Symbols

Unique and immutable data type.

const sym1 = Symbol("desc");

const sym2 = Symbol("desc");

console.log(sym1 === sym2); // Output: false 

Comments

Popular posts from this blog

Classes and Objects in Javascript | Lecture 10

Destructuring in JavaScript | Lecture 5