Frontend Development · Tutorial 11 of 11
JavaScript ES6 Features: arrow functions, destructuring and modules
By the FUTEAC Team · 16 November 2026 · 11 min read · Beginner
In short
ES6, the 2015 update to JavaScript, added shorter ways to write code you already know: arrow functions, template literals for building strings, destructuring for pulling values out of objects and arrays, and modules for splitting code across files. No new tool is needed. Node.js and every modern browser already run them, and this tutorial proves it line by line.
- ES6 is a version of JavaScript, not a separate language. ES6 means ECMAScript 2015, the update that added the features in this tutorial. Code written before it still runs, and code written with it runs everywhere modern.
constandletreplacevar. Useconstunless the value must change. Aconstyou try to reassign fails loudly, which is a feature, not a bug.- Arrow functions are shorter and keep the
thisfrom the code around them. That one difference is the reason they exist, and the reason they sometimes confuse people. - Destructuring pulls values out in one line. A missing property gives
undefined, not an error, so check for it. - Modules split code into files with
exportandimport, and the file name needs its extension. Forget the.mjsand Node.js cannot find the file at all. - This tutorial feeds the JavaScript Essentials course (/course/javascript-essentials) and the Modern JavaScript (ES6+) (/skill/javascript-es6) skill.
Before you start
What is ES6, and why does every tutorial use it?
Open any modern JavaScript tutorial and the code looks different from the JavaScript of 2010. Functions are written with =>, strings use backticks, variables are declared with const, and files start with import. That is ES6.
ES6 is short for ECMAScript 2015, the sixth edition of the language standard. It did not replace JavaScript: it added new syntax to it, the way a new edition of a dictionary adds words. The old ways still work, but the new ways are shorter and clearer, so every tutorial since, including the React tutorials later in this series, uses them.
One opinion, up front: learn these five features before you touch React. React code is written in ES6 from the first line, with arrow functions, destructuring and modules on every screen. If those look strange, React will look impossible. If they look familiar, React is just new ideas on top of tools you already hold.
How do const and let replace var?
Before ES6, every variable was declared with var. It still works, but it has a trap: a var declared inside a block, like an if or a loop, leaks out of that block and exists after it. let and const stay inside the block where they were declared, which is what most people expect.
if (true) {
var leaked = "I escape the block";
let kept = "I stay inside it";
}
console.log(leaked); // "I escape the block"
console.log(kept); // ReferenceError: kept is not definedThat is the whole reason var fell out of use. The rule for this tutorial is simple: use const by default, use let when the value must change, and do not write var in new code.
const has one strict rule worth meeting early. You can change what is inside a const object or array, but you cannot point the name at something else. Try, and Node.js stops you with the exact error from the test run:
const siteName = "FUTEAC";
siteName = "Other";TypeError: Assignment to constant variable.That error is your friend. It catches the typo where you meant to compare with === and typed = instead, and it tells you at once instead of letting the wrong value spread through your program.
How do arrow functions work?
A regular function written the old way looks like this:
function double(n) {
return n * 2;
}The arrow version says the same thing in one line:
const double = (n) => n * 2;
console.log(double(4)); // 8Read it left to right: parameters, arrow, result. When the body is a single expression, the return and the braces disappear. With no parameters, write empty brackets, () =>. With one parameter the brackets are optional, but keep them: (n) => reads better than n =>.
The output above, 8, is the real output from the test run. You will meet arrow functions most as short callbacks: the reduce call in section 6 passes one in, and the DOM tutorial's addEventListener calls take one too.
Arrow functions have one real difference, and it is the reason they exist. A regular function gets its own this, which changes depending on how it is called. An arrow function gets none: it borrows the one from the code around it. Like shorthand in handwriting, the short form works because the words around it supply the meaning.
The test run proved the borrowing with a small object that counts inside a timer:
const counter = {
count: 0,
start: function () {
setTimeout(() => {
this.count++;
console.log("arrow this.count:", this.count);
}, 10);
},
};
counter.start();The output was:
arrow this.count: 1The arrow function inside setTimeout still saw counter, because it borrowed this from start, where this is the counter object. A regular function in that spot would have lost it. The practical rule: use arrow functions for short callbacks inside methods and event handlers, and use regular functions when you need this to mean the thing that called the function.
How do template literals work?
Joining strings with + gets unreadable fast:
const name = "Ayesha";
const score = 87;
console.log(name + " scored " + score + " out of 100.");Template literals use backticks instead of quotes, and ${ } drops a value straight into the string:
const user = { name: "Ayesha", score: 87 };
console.log(`${user.name} scored ${user.score} out of 100.`);The test run output was:
Ayesha scored 87 out of 100.Anything that produces a value can go inside ${ }, including a calculation like ${score + 5} or a function call like ${greet("Ayesha")}. They can also span several lines, which makes them the natural choice for building HTML in JavaScript. Use backticks whenever a string contains a value, and save + for adding numbers.
How does destructuring work?
Destructuring pulls values out of an object or an array in a single line, instead of one line per value. Imagine unpacking a suitcase: instead of opening the bag for each item, you take everything out at once and put each thing where it belongs. That is what this line does:
const user = { name: "Ayesha", score: 87 };
const { name, score } = user;
console.log(name, score); // Ayesha 87The names on the left must match the property names on the right. You can rename one while you pull it out, with a colon:
const { name: studentName } = user;
console.log(studentName); // AyeshaArrays destructure by position, not by name:
const scores = [87, 92, 76];
const [first, second] = scores;
console.log(first, second); // 87 92Two details matter. You can skip positions with an empty slot: const [first, , third] = scores takes the first and third values. And destructuring a property that does not exist gives undefined, not an error, as the test run confirmed:
const { age } = user;
console.log(age); // undefinedThat is a quiet failure, and it is the one to watch for. If a value prints as undefined, check the spelling of the property name before you check anything else.
What are default parameters, spread and rest?
Three small features, one section, because they all answer the same question: how do I write a function that copes with missing or extra values?
Default parameters fill in a value when the caller passes nothing:
function welcome(name = "learner") {
return `Welcome, ${name}!`;
}
console.log(welcome()); // Welcome, learner!
console.log(welcome("Bilal")); // Welcome, Bilal!The spread operator, three dots before an array, unpacks it into individual values. It copies, it never changes the original:
const scores = [87, 92, 76];
const moreScores = [...scores, 95];
console.log(moreScores); // [ 87, 92, 76, 95 ]
console.log(scores); // [ 87, 92, 76 ]Rest parameters use the same three dots, but inside a function's brackets, where they gather extra arguments into an array:
function total(...numbers) {
return numbers.reduce((sum, n) => sum + n, 0);
}
console.log(total(10, 20, 30)); // 60Every output above is the real output from the test run, including the second console.log in the spread example, which proves the original array was not touched. Spread and rest look identical and do opposite jobs: spread unpacks, rest gathers. The place tells you which one it is. Outside a function's brackets, it spreads. Inside, it gathers.
How do modules work?
A module is a file that shares its code with other files. Think of a book: each chapter is a file, and import is the table of contents pointing at the chapter you need. Without modules, a big program is one long file, and finding anything in it is miserable. With modules, each file does one job, and the imports at the top say exactly where each piece came from.
Make two files in one folder. The first, greet.mjs, shares a function and a value:
export function greet(name) {
return `Hello, ${name}!`;
}
export const siteName = "FUTEAC";The second, main.mjs, brings them in and uses them:
import { greet, siteName } from "./greet.mjs";
console.log(greet("Ayesha")); // Hello, Ayesha!
console.log(siteName); // FUTEACRun it with Node.js:
node main.mjsThe test run output was:
Hello, Ayesha!
FUTEACThree details matter. The .mjs extension tells Node.js the file is a module; a .js file is treated as the older CommonJS format by default, and import in it needs extra setup. The import path starts with ./, meaning "in this folder", and ends with the full file name, extension included. And the names in braces must match the exported names exactly, which is why the next section's error is so common.
In a browser, the same idea needs a script tag with type="module":
<script type="module" src="main.js"></script>The browser rules are the same: the path needs its extension, and the file must be served over HTTP, not opened straight from disk, or the browser will refuse to load it.
The wrong turn: the missing file extension
Here is the error every beginner meets on their first module. Write the import without the file extension, as you would in many other languages:
import { greet } from "./greet";Node.js cannot find the file, and it fails with this error. The paths in the real error name your own folder; the ERR_MODULE_NOT_FOUND part is what to search for:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../greet' imported from '.../main.mjs'Read it plainly: Node.js looked for a file literally called greet, with no extension, and there is no such file. The fix is one word: add the extension back, ./greet.mjs, and the same code runs. Browsers behave the same way, which is why the rule is worth learning once here instead of once per tool.
A close cousin of this error is importing a name the file never exported:
SyntaxError: The requested module './greet.mjs' does not provide an export named 'farewell'That one is also from the test run, and the fix is the same kind of check: open the exporting file and read the exact name it exports, then spell it the same way in the import.
Putting it together: a scores app
Here is one small program that uses every feature in this tutorial, the exact code from the verified test run. The greet.mjs file is unchanged, and main.mjs is the full version:
import { greet, siteName } from "./greet.mjs";
// Arrow functions
const double = (n) => n * 2;
console.log(double(4)); // 8
// Template literals
const user = { name: "Ayesha", score: 87 };
console.log(`${user.name} scored ${user.score} out of 100.`);
// Destructuring an object
const { name, score } = user;
console.log(name, score); // Ayesha 87
// Destructuring an array
const scores = [87, 92, 76];
const [first, second] = scores;
console.log(first, second); // 87 92
// Default parameters
function welcome(name = "learner") {
return `Welcome, ${name}!`;
}
console.log(welcome()); // Welcome, learner!
console.log(welcome("Bilal")); // Welcome, Bilal!
// Spread
const moreScores = [...scores, 95];
console.log(moreScores); // [ 87, 92, 76, 95 ]
// Rest parameters
function total(...numbers) {
return numbers.reduce((sum, n) => sum + n, 0);
}
console.log(total(10, 20, 30)); // 60
// Modules
console.log(greet("Ayesha")); // Hello, Ayesha!
console.log(siteName); // FUTEACRun it with node main.mjs. The expected output, exactly as the test run printed it, is:
8
Ayesha scored 87 out of 100.
Ayesha 87
87 92
Welcome, learner!
Welcome, Bilal!
[ 87, 92, 76, 95 ]
60
Hello, Ayesha!
FUTEACTen lines, one per feature, and each line is a check you can run yourself. If a line prints something different, the comments beside each console.log tell you what it should be, so you can find the section that covers it.
". File name: javascript-es6-browser-console.png. Alt text: Browser console showing the template literal and spread examples with their outputs.]
Try it yourself
Extend the scores app. Add a third file, scores.mjs, that exports the scores array and a total function, then import both into main.mjs and print the total of all four scores, which should be 350. Then change main.mjs so it prints a sentence with a template literal: "Ayesha's four scores total 350." If Node.js answers with ERR_MODULE_NOT_FOUND, check the import path first: it needs ./ at the start and .mjs at the end, exactly as in section 8.
Common mistakes
- Forgetting the file extension in an import.
./greetfails withERR_MODULE_NOT_FOUND. Write./greet.mjs, with the extension, every time. - Reassigning a
const.siteName = "Other"fails withTypeError: Assignment to constant variable.Useletwhen the value must change, andconstwhen it must not. - Importing a name that was never exported. The error names the missing export:
does not provide an export named 'farewell'. Open the exporting file and copy the exact name. - Destructuring a property that does not exist. You get
undefined, silently. Check the spelling of the property name before you check anything else. - Writing
varout of habit. It works, but it leaks out of blocks, and every modern codebase usesconstandletinstead. New code should not containvarat all. - Using a regular function where an arrow function was needed. Inside a method or an event handler, a regular callback gets its own
thisand loses the object you meant. The arrow version borrows thethisaround it, which is usually what you want.
Frequently asked questions
What does ES6 stand for?
ECMAScript 2015. ECMAScript is the official name of the language standard, JavaScript is the name everyone uses, and 2015 is the year this edition was released. ES6 and ES2015 mean the same thing.
Do I need to install anything to use ES6?
No. Node.js and every modern browser have run these features for years. If your Node.js version is 18 or newer, everything in this tutorial works as written. The test run used Node.js 24.22.1.
Should I use arrow functions for everything?
No. They are best for short functions and callbacks. A long function with several steps reads better with function and a name. And never use one when you need this to mean the thing that called the function, because an arrow function borrows this from the code around it instead.
Why does my import need ./ at the start?
Because ./ means "look in this folder". Without it, Node.js treats the name as a package installed from npm, searches for it there, and fails. The ./ is what says the file is yours and sits beside the importing file.
What is the difference between export and export default?
A named export, export function greet, can share several things from one file, and the importer must use the exact name in braces. A default export, export default function greet, shares one main thing, and the importer can call it any name and writes no braces. This tutorial uses named exports only, because they are clearer while you are learning, and React code uses them heavily.
Follow the Frontend Developer Roadmap
Build modern, responsive websites and web apps with HTML, CSS, JavaScript and React. Free, step by step, at your pace.
View the Roadmap