admin管理员组

文章数量:1023885

Question

Although you can explicitly check if a value is true or false, it's a convention in JavaScript to test against all falsy values. For example, we can test if a variable value is falsy by testing if (value).

Code

function UnconventionalDefaults(params, defaultParams) {
  if (params === undefined) {
    params = defaultParams;
  }

  // function would do work here

  return params;
}

// Modify this function to set params to defaultParams if params
// is falsy
function moreConventionalDefaults(params, defaultParams) {
  // do a more conventional check here (check if params is falsy, 
  // and not just undefined
  if(params === undefined){
    params === defaultParams;
  }else if(params === null){
    params === defaultParams;
  }else if(params === ""){
    params === defaultParams;
  }else if(params === false){
    params === defaultParams;
  }

  return params;
}

Although I'm testing against all the falsy values, this code is not being accepted. What is it that I'm doing wrong? Is there a better way to do it?

Question

Although you can explicitly check if a value is true or false, it's a convention in JavaScript to test against all falsy values. For example, we can test if a variable value is falsy by testing if (value).

Code

function UnconventionalDefaults(params, defaultParams) {
  if (params === undefined) {
    params = defaultParams;
  }

  // function would do work here

  return params;
}

// Modify this function to set params to defaultParams if params
// is falsy
function moreConventionalDefaults(params, defaultParams) {
  // do a more conventional check here (check if params is falsy, 
  // and not just undefined
  if(params === undefined){
    params === defaultParams;
  }else if(params === null){
    params === defaultParams;
  }else if(params === ""){
    params === defaultParams;
  }else if(params === false){
    params === defaultParams;
  }

  return params;
}

Although I'm testing against all the falsy values, this code is not being accepted. What is it that I'm doing wrong? Is there a better way to do it?

Share Improve this question asked Dec 1, 2011 at 16:31 RafayRafay 24.3k5 gold badges22 silver badges27 bronze badges 1
  • 1 You forgot 0, which is also falsy. – helpermethod Commented Dec 1, 2011 at 16:34
Add a ment  | 

4 Answers 4

Reset to default 7
  • You cannot use === for assigning into params. Use = instead.

  • Also those are not all falsy values, you are missing 0 and NaN.

The whole method can be simplified into:

function moreConventionalDefaults(params, defaultParams) {
  return params || defaultParams;
}

params will be evaluated and if it is falsy then the defaultParams will be returned.

EDIT: Have a loot at great article Exploring JavaScript’s Logical OR Operator by Addy Osmani for more information.

Try this one, please

function moreConventionalDefaults(params, defaultParams) {
    if (!params) params = defaultParams;
    return params;
     // or just 
    return params || defaultParams;
}

The best way to test for all falsy values is to use an explicit if statement. For example

if (params) {
  // Truthy
} else {
  // Falsy 
}

I'll leave the rest to you.

I think the question is asking for;

function moreConventialDefaults(params, defaultParams) {
    if (!params) {
        params = defaultParams;
    }
}

An if statement executes the statement if the condition evaluates to true (i.e. is truthy), otherwise it'll execute the else condition.

So by negating the params parameter (!params), we're executing the statement only if the condition evaluates to false (i.e. falsy), which is what the question is asking for.

Question

Although you can explicitly check if a value is true or false, it's a convention in JavaScript to test against all falsy values. For example, we can test if a variable value is falsy by testing if (value).

Code

function UnconventionalDefaults(params, defaultParams) {
  if (params === undefined) {
    params = defaultParams;
  }

  // function would do work here

  return params;
}

// Modify this function to set params to defaultParams if params
// is falsy
function moreConventionalDefaults(params, defaultParams) {
  // do a more conventional check here (check if params is falsy, 
  // and not just undefined
  if(params === undefined){
    params === defaultParams;
  }else if(params === null){
    params === defaultParams;
  }else if(params === ""){
    params === defaultParams;
  }else if(params === false){
    params === defaultParams;
  }

  return params;
}

Although I'm testing against all the falsy values, this code is not being accepted. What is it that I'm doing wrong? Is there a better way to do it?

Question

Although you can explicitly check if a value is true or false, it's a convention in JavaScript to test against all falsy values. For example, we can test if a variable value is falsy by testing if (value).

Code

function UnconventionalDefaults(params, defaultParams) {
  if (params === undefined) {
    params = defaultParams;
  }

  // function would do work here

  return params;
}

// Modify this function to set params to defaultParams if params
// is falsy
function moreConventionalDefaults(params, defaultParams) {
  // do a more conventional check here (check if params is falsy, 
  // and not just undefined
  if(params === undefined){
    params === defaultParams;
  }else if(params === null){
    params === defaultParams;
  }else if(params === ""){
    params === defaultParams;
  }else if(params === false){
    params === defaultParams;
  }

  return params;
}

Although I'm testing against all the falsy values, this code is not being accepted. What is it that I'm doing wrong? Is there a better way to do it?

Share Improve this question asked Dec 1, 2011 at 16:31 RafayRafay 24.3k5 gold badges22 silver badges27 bronze badges 1
  • 1 You forgot 0, which is also falsy. – helpermethod Commented Dec 1, 2011 at 16:34
Add a ment  | 

4 Answers 4

Reset to default 7
  • You cannot use === for assigning into params. Use = instead.

  • Also those are not all falsy values, you are missing 0 and NaN.

The whole method can be simplified into:

function moreConventionalDefaults(params, defaultParams) {
  return params || defaultParams;
}

params will be evaluated and if it is falsy then the defaultParams will be returned.

EDIT: Have a loot at great article Exploring JavaScript’s Logical OR Operator by Addy Osmani for more information.

Try this one, please

function moreConventionalDefaults(params, defaultParams) {
    if (!params) params = defaultParams;
    return params;
     // or just 
    return params || defaultParams;
}

The best way to test for all falsy values is to use an explicit if statement. For example

if (params) {
  // Truthy
} else {
  // Falsy 
}

I'll leave the rest to you.

I think the question is asking for;

function moreConventialDefaults(params, defaultParams) {
    if (!params) {
        params = defaultParams;
    }
}

An if statement executes the statement if the condition evaluates to true (i.e. is truthy), otherwise it'll execute the else condition.

So by negating the params parameter (!params), we're executing the statement only if the condition evaluates to false (i.e. falsy), which is what the question is asking for.

本文标签: javascriptConventional boolean testsStack Overflow