MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Default Parameters0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
HomeTutorialsJavaScript for IntermediateDefault Parameters
Enhanced Object Literals

Default Parameters

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Before ES6, handling missing function parameters required defensive checks inside the function body. Modern JavaScript allows developers to define Default Parameters directly in the function declaration header.


1. The Legacy Way vs. The Modern ES6 Way

Legacy Approach (ES5):

1function sendEmail(to, subject, priority) {
2 // Defensive manual fallbacks:
3 subject = typeof subject !== "undefined" ? subject : "No Subject";
4 priority = typeof priority !== "undefined" ? priority : "normal";
5 
6 console.log(`Sending to: ${to} | Subject: ${subject} | Priority: ${priority}`);
7}

Modern ES6 Default Parameters:

1function sendEmail(to, subject = "No Subject", priority = "normal") {
2 console.log(`Sending to: ${to} | Subject: ${subject} | Priority: ${priority}`);
3}
4 
5sendEmail("sumit@mskinstitute.com");
6// "Sending to: sumit@mskinstitute.com | Subject: No Subject | Priority: normal"

2. Default Parameters Only Trigger on undefined

[!IMPORTANT] A default parameter is applied ONLY if the argument passed is undefined (or completely omitted). Values like null, 0, false, or "" are treated as valid, explicit inputs and will NOT trigger the default!
1function setVolume(level = 50) {
2 console.log(`Volume: ${level}`);
3}
4 
5setVolume(); // Volume: 50 (Omitted -> default)
6setVolume(undefined); // Volume: 50 (undefined -> default)
7setVolume(null); // Volume: null (null is explicit! Default NOT triggered!)
8setVolume(0); // Volume: 0 (0 is a valid number! Default NOT triggered!)
9setVolume(80); // Volume: 80

3. Dynamic and Evaluated Defaults

In JavaScript, default parameter values are evaluated at run-time at the moment of function invocation:

  • You can call other functions inside defaults.
  • Later parameters can reference earlier parameters in the same signature!
1function generateId() {
2 return Math.random().toString(36).substring(2, 9);
3}
4 
5function createUser(name, id = generateId(), timestamp = Date.now()) {
6 return { name, id, timestamp };
7}
8 
9console.log(createUser("Sumit"));
10// { name: "Sumit", id: "k7d8f9a", timestamp: 1773534800000 }

Referencing Earlier Parameters:

1function createDimensions(width, height = width * 1.5) {
2 return { width, height };
3}
4 
5console.log(createDimensions(100)); // { width: 100, height: 150 }

4. Combining Default Parameters with Destructuring

1// Destructuring with default object fallback to prevent "Cannot read properties of undefined"
2function connectToDatabase({ host = "localhost", port = 5432, dbName = "msk_db" } = {}) {
3 console.log(`Connected to ${host}:${port}/${dbName}`);
4}
5 
6// Can be called with partial config:
7connectToDatabase({ host: "db.aws.com" }); // Connected to db.aws.com:5432/msk_db
8 
9// Can be called with ZERO arguments without throwing!
10connectToDatabase(); // Connected to localhost:5432/msk_db

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
When is a default parameter value applied in JavaScript?
Question 2
What is the output of function multiply(a, b = 2) { return a * b; } multiply(5, null);?
Question 3
When are default parameter expressions evaluated in JavaScript?
Question 4
Can a later default parameter use the value of an earlier parameter in the same function declaration?
Question 5
Why is = {} added at the end of function config({ port = 3000 } = {})?
Next Lesson

Enhanced Object Literals

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Destructuring Arrays and ObjectsEnhanced Object Literals

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext