How To Set Default Parameter Value for a function in JavaScript

Опубликовано: 15 Май 2026
на канале: Profu' de geogra'
2
0

How to Set Default Parameter Values in JavaScript | Learn JavaScript Easily!

In this tutorial, we’ll walk you through how to set default parameter values in JavaScript functions! 🚀

When writing functions, sometimes you might want to provide default values for parameters in case the user doesn’t supply one. This can help your code run more smoothly, avoiding potential errors or undefined values. In this video, we break down the concept using an easy-to-follow example!

What you’ll learn in this video:

What default parameters are and why they are useful
How to set default parameter values in a function
How default values make your code more flexible and prevent errors
A real-life example of setting a default parameter in a greet function
Example Explained: We create a greet function with two parameters: name and age. If no age is passed, the function will automatically use the default value of 30. Watch how it works when we call the function with both provided values, and when we call it with just the name!