admin管理员组文章数量:1024074
I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
-
What else do you consider to be a
truthy
value? – VisioN Commented Apr 21, 2016 at 11:39 - @RayonDabre You didn't understand the question. Your code will always return a boolean rather than the actual variable if it's not false. – VisioN Commented Apr 21, 2016 at 11:40
- @VisioN What do you mean? – Chrillewoodz Commented Apr 21, 2016 at 11:41
-
If you consider
0
to betruthy
, what otherfalthy
values do you consider to betruthy
as well? Or0
is the only exception? – VisioN Commented Apr 21, 2016 at 11:43 - @VisioN 0 is the only exception, which always cause bugs in my code. – Chrillewoodz Commented Apr 21, 2016 at 11:43
4 Answers
Reset to default 3A solution with logical operators only:
function getValue(v) {
return v !== 0 && (v || 'someString') || 0;
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
Another proposal
function getValue(v) {
return v || v === 0 ? v : 'someString';
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
My solution:
(myVariable.toString() || 'somestring')
You'll need a try/catch though
Try this:
x === 0 || x ? x : y
Gives same output as:
x !== 0 && (x || y) || 0;
but is easier to read I think
i found easy solution:
( myVariable === 0 ? '0' : ( myVariable || 'somestring' ) )
I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
-
What else do you consider to be a
truthy
value? – VisioN Commented Apr 21, 2016 at 11:39 - @RayonDabre You didn't understand the question. Your code will always return a boolean rather than the actual variable if it's not false. – VisioN Commented Apr 21, 2016 at 11:40
- @VisioN What do you mean? – Chrillewoodz Commented Apr 21, 2016 at 11:41
-
If you consider
0
to betruthy
, what otherfalthy
values do you consider to betruthy
as well? Or0
is the only exception? – VisioN Commented Apr 21, 2016 at 11:43 - @VisioN 0 is the only exception, which always cause bugs in my code. – Chrillewoodz Commented Apr 21, 2016 at 11:43
4 Answers
Reset to default 3A solution with logical operators only:
function getValue(v) {
return v !== 0 && (v || 'someString') || 0;
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
Another proposal
function getValue(v) {
return v || v === 0 ? v : 'someString';
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
My solution:
(myVariable.toString() || 'somestring')
You'll need a try/catch though
Try this:
x === 0 || x ? x : y
Gives same output as:
x !== 0 && (x || y) || 0;
but is easier to read I think
i found easy solution:
( myVariable === 0 ? '0' : ( myVariable || 'somestring' ) )
本文标签: javascriptHow to make a zero number value count as a truthy valueStack Overflow
版权声明:本文标题:javascript - How to make a zero number value count as a truthy value? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745535533a2154943.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论