- Use template literals for string interpolation
- Destructure arrays and objects
- Apply the spread operator effectively
- Set default function parameters
- Understand
letandconstscope rules
BMC201 - Web Technology
2026-02-25
Lecture 19
ES6+ Features
Week 5 | Unit II: DOM Manipulation
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor
Learning Objectives
let and const scope rulesWhat is ES6+?
ES6 (ECMAScript 2015) introduced major improvements to JavaScript:
let and constTemplate Literals
const name = "Priya";
const age = 22;
const message = `Hello, ${name}! You are ${age} years old.`;
console.log(message);Use backticks (`) and ${} for dynamic values.
Multi-line Strings
const poem = `Roses are red,
Violets are blue,
JavaScript rocks,
And so do you!`;
console.log(poem);No need for \n or concatenation.
let and const
Block Scope with let/const
let and const are block-scoped, unlike var.
Default Function Parameters
function greet(name = "Guest") {
return `Hello, ${name}`;
}
console.log(greet()); // Hello, Guest
console.log(greet("Neha")); // Hello, NehaProvide fallback values when arguments are missing.
Array Destructuring
const colors = ['red', 'green', 'blue'];
const [first, second, third] = colors;
console.log(first); // red
const [a, , c] = colors;
console.log(a, c); // red blueExtract values from arrays into variables.
Object Destructuring
const student = { name: "Asha", age: 21, course: "CSE" };
const { name, age } = student;
console.log(name, age); // Asha 21
const { name: studentName, age: studentAge } = student;
console.log(studentName); // AshaExtract properties from objects.
Spread Operator: Arrays
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
console.log(arr2); // [1, 2, 3, 4, 5]
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 1, 2, 3, 4, 5]Spread (...) expands array elements.
Spread Operator: Objects
const user = { name: "Raj", age: 25 };
const updatedUser = { ...user, age: 26 };
console.log(updatedUser); // { name: "Raj", age: 26 }
const merged = { ...user, city: "Delhi" };
console.log(merged); // { name: "Raj", age: 25, city: "Delhi" }Copy and merge objects easily.
Rest Parameters
function sum(...numbers) {
return numbers.reduce((acc, n) => acc + n, 0);
}
console.log(sum(1, 2, 3, 4)); // 10Rest (...) collects arguments into an array.
Combining Arrow Functions & Destructuring
Ternary Operator Refresher
Shorthand for if...else.
Short-circuit Evaluation
const username = user.name || "Guest";
const display = isLoggedIn && <Dashboard />;
const firstName = user?.name?.first;
const count = items?.length ?? 0;|| returns first truthy value&& returns second if first is truthy?. optional chaining?? nullish coalescingES6 Modules (Preview)
// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
// app.js
import { add, subtract } from './math.js';Organize code into reusable modules.
Practice Example
Common Mistakes
var instead of let/constconst primitive values... with rest ...Practice Task
Questions?
Next: Lecture 20 - Revision: Units I & II