How to add Default function parameters in JavaScript

by Sai gowtham  1 Min read

Default function parameters help us to initialize the parameters with the default values.

By default function parameters are initialized with the value undefined if we don’t pass any value to the function.

Let’s see in practice.

Without default parameters

function enterYourname(name){

    return 'User name is '+ name



In the above example we don’t pass any argument to the function so that it will return username is undefined.

default parameters JavaScript

With default parameters

function enterYourname(name="unknown"){

    return 'User name is '+ name



Here we added a default value to unknown.

default-parameters JavaScript

More examples

function add(a,b=5){

    return a+b;


// output -> 6
function combineArrays(a,b=[5,6,7]){

    return [...a,...b];



// output [1,2,3,4,5,6,7]