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
4 Answers
Reset to default 7You cannot use
===
for assigning into params. Use=
instead.Also those are not all falsy values, you are missing
0
andNaN
.
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
4 Answers
Reset to default 7You cannot use
===
for assigning into params. Use=
instead.Also those are not all falsy values, you are missing
0
andNaN
.
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
版权声明:本文标题:javascript - Conventional boolean tests - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745601257a2158470.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论