Can someone tell me how can we do this?

//How can assign b to 5?

function fun(a = 10,b) {

  console.log(a, b)

Your function takes 2 parameters. Thus, if you want to give b a value, you have to pass an argument to a first.

fun(undefined, 5)

Thanks for reply any other way to do that?

Nope. No other way unless you only give your function 1 parameter.

If you want be to b to be 5 by default (meaning a second argument is not passed to the function), then you can create a default parameter value for b similar to the one for a in the function declaration. Then, if you call the function like:

fun(7); // displays "7 5" in the console.

b would still have the value 5 in the console.log statement.

Just making it clearer to you, what they said:

function fun(a = 10, b=5){
    console.log (a , b)

The point is: if you want to b=5, you should define it on the creation of your function, like above. But, this doesn’t mean that a = 10 and b = 5 forever. You can pass other values as well and it will still work. Javascript is very flexible with parameters, though you can call your function like this:

fun(5, 6)

wich will result in a console like this 5 6.

But you can call the function without any parameters(that’s when your a = 10 and b = 5 will take place). Look:


will display 10 5

Hope it helps you.