Modern JavaScript (ES6+)
The modern features you will use in every React and Node project: destructuring, spread, and more.
What you will learn
- Use destructuring for arrays and objects
- Use the spread operator
- Recognise modern syntax in real code
What does “ES6+” mean?
JavaScript gets new features over time, released as numbered versions. ES6 is short for ECMAScript 2015 — a big 2015 update that added many of the handy tools we use today (ECMAScript is just the official name for the JavaScript standard). The little + means "ES6 and everything added since". So "modern JavaScript (ES6+)" simply means the newer, nicer way of writing JS — nothing scary, just shortcuts you will love.
Destructuring — unpack values
Pull values out of arrays and objects into variables in one line.
<script>
const user = { name: "Asha", age: 22, city: "Bengaluru" };
const { name, city } = user; // unpack object
document.write(name + " from " + city + "<br>");
const colors = ["red", "green", "blue"];
const [first, second] = colors; // unpack array
document.write(first + ", " + second);
</script>const { name, city } = user reaches into the object and pulls those two properties into their own variables in one line (we ignored age). For arrays, const [first, second] = colors pulls items out by position — first gets index 0 ("red"), second gets index 1 ("green").
Note: Output: Asha from Bengaluru red, green
Spread — copy and combine
The spread operator ... copies or merges arrays and objects.
<script>
const a = [1, 2, 3];
const b = [...a, 4, 5]; // copy a, then add more
document.write(b.join(", ") + "<br>");
const base = { role: "student" };
const asha = { ...base, name: "Asha" }; // merge objects
document.write(asha.name + " is a " + asha.role);
</script>[...a, 4, 5] means "pour all of a in here, then add 4 and 5", giving a new array [1, 2, 3, 4, 5]. The same dots work on objects: { ...base, name: "Asha" } copies everything from base (the role) and adds a name, building a new merged object.
Note: Output: 1, 2, 3, 4, 5 Asha is a student
Tip: Destructuring and spread are everywhere in React — props are destructured, state is updated with spread. Master them now and React will feel natural.
Q. What does const { name } = user do?
✍️ Practice
- Destructure three properties out of an object in one line.
- Use spread to merge two arrays, then to copy an object and add a property.
🏠 Homework
- Rewrite some earlier code to use destructuring and spread where it helps.