Tutorials Logic, IN info@tutorialslogic.com

JavaScript ES6 Features Arrow Functions, Destructuring, Spread

JavaScript ES6 Features Arrow Functions, Destructuring, Spread

es_6 is an important JavaScript topic because it shows up in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.

Focus on what problem es_6 solves, where developers usually make mistakes, and how to verify the result with output, behavior, or a small test.

A strong understanding of es_6 should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work.

JavaScript ES6 Features Arrow Functions Destructuring Spread should be studied as a practical JavaScript lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.

In the javascript > es-6 page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.

JavaScript ES6 / ECMAScript 2015

ES6, officially called ECMAScript 2015, was one of the biggest upgrades in JavaScript history. It introduced modern syntax and language features that make JavaScript easier to write, easier to read, and better suited for large applications.

Most modern JavaScript code uses ES6 every day. If you work with React, Vue, Angular, Node.js, npm packages, bundlers, or browser APIs, you will constantly see let, const, arrow functions, template literals, destructuring, classes, modules, promises, and spread/rest syntax.

  • ES6 introduced block-scoped variables with let and const.
  • It added cleaner syntax for functions, strings, objects, arrays, classes, and modules.
  • It made asynchronous programming easier with Promise.
  • It gave JavaScript better tools for building maintainable applications.

ES6 Feature Overview

The table below summarizes the most important ES6 features and why they matter.

Feature Purpose Example
let, const Block-scoped variable declarations const name = "Asha";
Template literals String interpolation and multi-line strings `Hello ${name}`
Arrow functions Shorter function syntax with lexical this n => n * 2
Default parameters Fallback values for function arguments function greet(name = "Guest")
Rest and spread Collect or expand values sum(...numbers)
Destructuring Extract array/object values into variables const { id } = user;
Enhanced objects Shorter object property and method syntax { name, login() {} }
Classes Cleaner syntax for constructor/prototype patterns class User {}
Modules Import and export code between files export default App;
Promises Handle asynchronous results fetch(url).then(...)

let, const, and Block Scope

Before ES6, JavaScript mainly used var. A var variable is function-scoped, which can create confusing bugs inside blocks and loops. ES6 added let and const, which are block-scoped. A block is the area inside curly braces, such as an if statement, loop, or function body.

Use const by default when the variable should not be reassigned. Use let when the value needs to change. Avoid var in modern JavaScript unless you are maintaining older code.

const prevents reassignment, but it does not make objects or arrays immutable. You can still change the contents of an object declared with const.

Block Scope

Block Scope
const course = "JavaScript ES6";
let lesson = 1;

if (lesson === 1) {
  const topic = "let and const";
  lesson = lesson + 1;
  console.log(topic); // let and const
}

console.log(course); // JavaScript ES6
console.log(lesson); // 2
// console.log(topic); // ReferenceError: topic is not defined

const with Objects

const with Objects
const user = {
  name: "Riya",
  role: "Student"
};

user.role = "Developer"; // allowed
user.city = "Pune";      // allowed

// user = {}; // TypeError: Assignment to constant variable

Template Literals

Template literals use backticks instead of single or double quotes. They support interpolation with ${...}, multi-line strings, and readable dynamic text. They are very useful for messages, HTML snippets, logs, and generated URLs.

Template Literals

Template Literals
const name = "Kabir";
const score = 92;

const message = `Hello ${name}, your score is ${score}%.`;
console.log(message);

const card = `
  <article class="student-card">
    <h2>${name}</h2>
    <p>Score: ${score}%</p>
  </article>
`;

Arrow Functions

Arrow functions give JavaScript a compact way to write functions. They are common in callbacks, array methods, promises, and component code. If an arrow function has one parameter, parentheses are optional. If the body is a single expression, the result is returned automatically.

Arrow functions do not create their own this. Instead, they use this from the surrounding scope. This is useful in many callbacks, but arrow functions are usually not the right choice for object methods that need their own receiver.

Arrow Function Syntax

Arrow Function Syntax
const double = number => number * 2;
const add = (a, b) => a + b;

const greet = name => {
  const message = `Welcome, ${name}`;
  return message;
};

console.log(double(6));    // 12
console.log(add(10, 20));  // 30
console.log(greet("Maya"));

Arrow this

Arrow this
const timer = {
  seconds: 0,
  start() {
    setInterval(() => {
      this.seconds++;
      console.log(this.seconds);
    }, 1000);
  }
};

// timer.start();

Default Parameters

Default parameters let you define fallback values directly in the function signature. This avoids extra checks inside the function body and makes intent clear.

Default Parameters

Default Parameters
function createUser(name = "Guest", role = "Reader") {
  return {
    name,
    role,
    active: true
  };
}

console.log(createUser());
console.log(createUser("Anika", "Admin"));

Rest Parameters

The rest parameter syntax collects remaining function arguments into an array. It is useful when a function can receive any number of values.

Rest Parameters

Rest Parameters
function sum(...numbers) {
  return numbers.reduce((total, number) => total + number, 0);
}

console.log(sum(5, 10));       // 15
console.log(sum(1, 2, 3, 4));  // 10

Spread Syntax

Spread syntax also uses three dots, but it expands arrays or objects instead of collecting values. Use it to copy arrays, merge arrays, pass array values as arguments, or build new objects without mutating the original.

Spread Syntax

Spread Syntax
const frontend = ["HTML", "CSS"];
const javascript = ["JavaScript", "TypeScript"];

const skills = [...frontend, ...javascript];
console.log(skills);

const user = { name: "Ishaan", role: "Student" };
const updatedUser = { ...user, role: "Developer", active: true };

console.log(updatedUser);

Array Destructuring

Destructuring extracts values from arrays or objects and assigns them to variables. Array destructuring is based on position.

Array Destructuring

Array Destructuring
const colors = ["red", "green", "blue"];

const [primary, secondary] = colors;
console.log(primary);   // red
console.log(secondary); // green

const [first, , third] = colors;
console.log(third); // blue

Object Destructuring

Object destructuring extracts values by property name. It is heavily used with function parameters, configuration objects, API responses, and imports from libraries.

Object Destructuring

Object Destructuring
const student = {
  id: 101,
  name: "Nisha",
  address: {
    city: "Hyderabad"
  }
};

const { id, name } = student;
const { address: { city } } = student;

console.log(id, name, city);

Destructuring in Parameters

Destructuring in Parameters
function printProfile({ name, role = "User" }) {
  console.log(`${name} works as ${role}`);
}

printProfile({ name: "Dev", role: "Frontend Engineer" });
printProfile({ name: "Sana" });

Enhanced Object Literals

ES6 improved object literal syntax. If the variable name and property name are the same, you can write the name once. You can also write methods without the function keyword and create dynamic property names with computed keys.

Enhanced Objects

Enhanced Objects
const name = "Task Manager";
const version = "1.0";
const dynamicKey = "createdAt";

const app = {
  name,
  version,
  [dynamicKey]: new Date().toISOString(),
  start() {
    console.log(`${this.name} started`);
  }
};

app.start();

for...of Loop

The for...of loop iterates over iterable values such as arrays, strings, maps, sets, and NodeLists. It gives you the value directly, unlike for...in, which iterates over property keys.

for...of

for...of
const languages = ["JavaScript", "Python", "PHP"];

for (const language of languages) {
  console.log(language);
}

for (const letter of "ES6") {
  console.log(letter);
}

Map and Set

ES6 introduced new collection types. A Map stores key-value pairs and allows any value to be used as a key. A Set stores unique values and automatically removes duplicates.

Map and Set

Map and Set
const scores = new Map();
scores.set("Aarav", 95);
scores.set("Meera", 88);

console.log(scores.get("Aarav")); // 95
console.log(scores.has("Meera")); // true

const tags = new Set(["js", "web", "js", "frontend"]);
console.log([...tags]); // ["js", "web", "frontend"]

Classes

ES6 classes provide a clean syntax for creating objects with shared behavior. A class is syntactic sugar over JavaScript's prototype system. It does not replace prototypes; it makes constructor functions and prototype methods easier to read.

Classes can also inherit from other classes using extends and call the parent constructor with super().

Classes

Classes
class User {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }

  login() {
    return `${this.name} logged in`;
  }
}

const user = new User("Tara", "tara@example.com");
console.log(user.login());

Class Inheritance

Class Inheritance
class Admin extends User {
  constructor(name, email, permissions) {
    super(name, email);
    this.permissions = permissions;
  }

  canEdit() {
    return this.permissions.includes("edit");
  }
}

const admin = new Admin("Rohan", "rohan@example.com", ["edit", "delete"]);
console.log(admin.login());
console.log(admin.canEdit());

Modules: import and export

ES6 modules let you split code into separate files. A module can export variables, functions, classes, or a default value. Other files can import only what they need. Modules make code easier to organize and help avoid global variables.

Named Exports

Named Exports
// math.js
export const pi = 3.14159;

export function square(number) {
  return number * number;
}

// app.js
import { pi, square } from "./math.js";

console.log(pi);
console.log(square(5));

Default Export

Default Export
// logger.js
export default function log(message) {
  console.log(`[App] ${message}`);
}

// app.js
import log from "./logger.js";

log("Application started");

Promises

A Promise represents the eventual result of an asynchronous operation. It starts in the pending state, then becomes fulfilled or rejected. Promises replaced many callback-heavy patterns and became the foundation for modern async JavaScript.

Promise Basics

Promise Basics
const loadData = new Promise((resolve, reject) => {
  const success = true;

  if (success) {
    resolve("Data loaded");
  } else {
    reject(new Error("Failed to load data"));
  }
});

loadData
  .then(result => console.log(result))
  .catch(error => console.error(error.message));

Useful ES6 String and Array Methods

ES6 also added helpful methods to strings and arrays. These methods make everyday checks and transformations cleaner.

String Methods

String Methods
const title = "JavaScript ES6 Tutorial";

console.log(title.startsWith("JavaScript")); // true
console.log(title.endsWith("Tutorial"));     // true
console.log(title.includes("ES6"));          // true
console.log("Hi ".repeat(3));                // Hi Hi Hi

Array Methods

Array Methods
const numbers = [5, 12, 8, 130, 44];

const firstLarge = numbers.find(number => number > 10);
const firstLargeIndex = numbers.findIndex(number => number > 10);

console.log(firstLarge);      // 12
console.log(firstLargeIndex); // 1
console.log(Array.from("ES6")); // ["E", "S", "6"]

Symbols

A Symbol is a unique primitive value. Symbols are often used as special object keys when you want to avoid accidental name conflicts.

Symbol

Symbol
const id = Symbol("id");

const user = {
  name: "Leena",
  [id]: 123
};

console.log(user[id]); // 123
console.log(Symbol("id") === Symbol("id")); // false

Generators

Generator functions can pause and resume execution with yield. They return an iterator, which means you can request values one at a time. Generators are useful for custom iteration, lazy sequences, and advanced async patterns.

Generator Function

Generator Function
function* idGenerator() {
  let id = 1;

  while (true) {
    yield id++;
  }
}

const ids = idGenerator();

console.log(ids.next().value); // 1
console.log(ids.next().value); // 2
console.log(ids.next().value); // 3

ES6 Practice Example

The following example combines several ES6 features in one practical piece of code: const, default parameters, destructuring, template literals, arrow functions, spread syntax, and array methods.

Putting ES6 Together

Putting ES6 Together
const students = [
  { name: "Asha", marks: 82 },
  { name: "Vikram", marks: 74 },
  { name: "Neha", marks: 91 }
];

const addGrade = ({ name, marks }, passingMarks = 75) => ({
  name,
  marks,
  status: marks >= passingMarks ? "Passed" : "Needs Practice"
});

const result = students.map(student => addGrade(student));

const topStudent = result.find(({ marks }) => marks > 90);

console.log([...result]);
console.log(`${topStudent.name} scored above 90 marks.`);

Conclusion

ES6 changed JavaScript from a small scripting language into a much more expressive language for serious application development. Once you understand let, const, arrow functions, template literals, destructuring, spread/rest syntax, classes, modules, and promises, most modern JavaScript code becomes much easier to read.

The best way to master ES6 is to use these features together. Start by replacing old var declarations, practice array and object destructuring, write small modules, and build simple examples with promises. These patterns appear everywhere in modern frontend and backend JavaScript.

JavaScript ES6 Features Arrow Functions Destructuring Spread Java review example

JavaScript ES6 Features Arrow Functions Destructuring Spread Java review example
class JavaScriptES6FeaturesArrowFunctionsDestructuringSpreadReview {
    public static void main(String[] args) {
        String state = "ready";
        System.out.println("JavaScript ES6 Features Arrow Functions Destructuring Spread: " + state);
    }
}

JavaScript ES6 Features Arrow Functions Destructuring Spread guard example

JavaScript ES6 Features Arrow Functions Destructuring Spread guard example
String value = null;
if (value == null) {
    System.out.println("JavaScript ES6 Features Arrow Functions Destructuring Spread: handle the missing value before continuing");
}
Key Takeaways
  • Explain the purpose of es_6 before memorizing syntax.
  • Trace the exact call expression and confirm which value reached the parentheses.
  • Test one normal case, one edge case, and one mistake case for es_6.
  • Write down why the value is not callable and what should hold the function instead.
  • Connect es_6 to a real project scenario instead of treating it as an isolated definition.
Common Mistakes to Avoid
WRONG Calling a value before checking whether it actually holds a function reference.
RIGHT Trace the variable assignment, the property lookup, and the actual call expression.
Most beginner errors come from skipping the behavior behind the syntax.
WRONG Memorizing JavaScript ES6 Features Arrow Functions Destructuring Spread without the situation where it is useful.
RIGHT Connect JavaScript ES6 Features Arrow Functions Destructuring Spread to a concrete JavaScript task.
Purpose makes syntax easier to recall.
WRONG Testing JavaScript ES6 Features Arrow Functions Destructuring Spread only with the perfect input.
RIGHT Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Real bugs usually appear outside the perfect path.
WRONG Memorizing JavaScript ES6 Features Arrow Functions Destructuring Spread without the situation where it is useful.
RIGHT Connect JavaScript ES6 Features Arrow Functions Destructuring Spread to a concrete JavaScript task.
Purpose makes syntax easier to recall.

Practice Tasks

  • Modify the example so it guards with `typeof` or uses the correct method name.
  • Write one mistake related to es_6, then fix it and explain the fix.
  • Summarize when to use es_6 and when another approach is better.
  • Write a small example that uses JavaScript ES6 Features Arrow Functions Destructuring Spread in a realistic JavaScript scenario.
  • Change one important value in the JavaScript ES6 Features Arrow Functions Destructuring Spread example and predict the result first.

Frequently Asked Questions

ES6 is a major part of modern JavaScript, but modern JavaScript also includes newer yearly ECMAScript updates such as async/await, optional chaining, nullish coalescing, and more.

Use <code>const</code> by default. Use <code>let</code> when a variable must be reassigned. Avoid <code>var</code> in new code because its function scope and hoisting behavior can be confusing.

JavaScript classes are syntax built on top of prototypes. They provide a familiar class-style structure, but JavaScript still uses prototypal inheritance internally.

Modern browsers support the core ES6 features. Older browsers may need transpilation with tools such as Babel, especially for production applications that support legacy users.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.