Tutorials Modern JavaScript (ES6+) for Beginners
Default Parameters & Object Shorthand
Learn Default Parameters & Object Shorthand in our free Modern JavaScript (ES6+) for Beginners series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.
On this page
Default Parameters & Object Shorthand
ES6 lets you give function parameters default values and write shorter object literals. Less boilerplate, fewer if (x === undefined) checks.
Default parameters
function createUser(name, role = 'student', active = true) {
return { name, role, active };
}
createUser('Alex'); // role student, active true
createUser('Admin', 'admin'); // active still true
createUser('Guest', 'guest', false);
Object property shorthand
const name = 'Node.js Chat Course';
const price = 499;
const currency = 'INR';
// Old
const product1 = { name: name, price: price, currency: currency };
// ES6 shorthand
const product2 = { name, price, currency };
Method shorthand in objects
const api = {
baseUrl: 'https://api.toolliyo.com',
getHealth() {
return fetch(this.baseUrl + '/health');
}
};
function buildEmail({ to, subject, body, cc = [] }) {
return { to, subject, body, cc, sentAt: new Date().toISOString() };
}
buildEmail({
to: 'student@example.com',
subject: 'Welcome to Toolliyo',
body: 'Start with ES6 lesson 1.'
});fn(null) does not use default — pass undefined or omit the argument.Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!