💻ES6 tutorial: default parameter in javascript

Hello, I am Rafi kadir and I am from Bangladesh. Taking Web Development as a profession not only fulfills my pocket but also my heart because it has been my passion since my early teenage.
JavaScript’s default parameters allow you to set a default value in case no value, or an undefined value, is provided when calling a function. This can be useful to avoid errors and make your code more concise.
- To define a default parameter, you can assign a value to the parameter in the function declaration. Here’s an example:
function greet(name = "Anonymous") {
console.log("Hello, " + name );
}
greet(); // Output: Hello, Anonymous
If you call the function without providing an argument for the name, it will use the default value.
- However, if you pass a value when calling the function, it will override the default value:
function greet(name = "Anonymous") {
console.log("Hello, " + name + "!");
}
greet("John"); // Output: Hello, John!
Here we passed the value of the name and it overrides the default value.
- We can use the return value of a function as a default value for a parameter:
let taxRate = () => 0.1;
let getPrice = function( price, tax = price * taxRate() ) {
return price + tax;
}
let fullPrice = getPrice(100);
console.log(fullPrice); //Output 110
![JavaScript DOM cheat sheet [PDF]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1702665519324%2F27950dbe-8e1f-462d-9936-5bd00f847b62.jpeg&w=3840&q=75)


