Skip to content
Weibo's Home
Go back

Javascript function paramter default value

Edit page
function f(s)
{
	var a = (s == undefined) ? 'qq' : s;
}

Can be rewrited as following code

function f(s)
{
	var a = s || 'qq';
}

First statement will evaluate value of s is true or not.

If true, s is assigned to a.

Else, statement will find next value a.k.a. ‘qq’.

‘qq’ will be assigned to a.

For further, you could directly write code like this in ES6.

function f(s = 'qq')
{
}

Edit page
Share this post:

Previous Post
I don't know c language
Next Post
My favorite script