javascript - How to make a zero number value count as a truthy value? - Stack Overflow

I've found myself running into the same buggy thing over and over the last couple of weeks, it

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.

Share Improve this question edited Apr 21, 2016 at 11:45 Chrillewoodz asked Apr 21, 2016 at 11:38 ChrillewoodzChrillewoodz 28.4k23 gold badges100 silver badges187 bronze badges 5
  • 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 be truthy, what other falthy values do you consider to be truthy as well? Or 0 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
Add a ment  | 

4 Answers 4

Reset to default 3

A 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' ) )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745535535a4631888.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信