最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论