Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct way to define default params in es5 #47

Open
redacted-dev opened this issue Jun 30, 2017 · 1 comment
Open

correct way to define default params in es5 #47

redacted-dev opened this issue Jun 30, 2017 · 1 comment

Comments

@redacted-dev
Copy link

I think this way is better:

'use strict';

function greet(msg, name) {
    msg = msg || "hello";
    name = name || "world";
    console.log(msg, name);
}

greet();
// -> hello world
greet('hey');
// -> hey world
@hville
Copy link

hville commented Jun 30, 2017

Only in the very narrow use case where arguments are always truthy. Fails with valid falsy arguments:

  • greet('',0) // -> "hello world" instead of "0"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants