开发者

What does "value || 0" mean? [duplicate]

开发者 https://www.devze.com 2023-01-17 23:30 出处:网络
This question already exists: 开发者_StackOverflow社区 Closed 12 years ago. Possible Duplicate: In Javascript, what does it mean when there is a logical operator in a variable declaration?
This question already exists: 开发者_StackOverflow社区 Closed 12 years ago.

Possible Duplicate:

In Javascript, what does it mean when there is a logical operator in a variable declaration?

Stumbled across this code online, have no idea what it does, wasn't able to find out with FireBug either, and I can't google it because of the special characters...

var myValue = myInput.value || 0;


if myInput.value is undefined (or another falsy value) then the default value of 0 will be set

some examples...

myInput.value = undefined
var myValue = myInput.value || 0;
// myValue = 0

myInput.value = 10
var myValue = myInput.value || 0;
// myValue = 10


|| is often called the default operator. It lets you give a default value, if something else is falsy. So if myInput.value is undefined, for example, myValue will be assigned 0.


Because of the way the javascript interpreter is built, it can interpret certain classes of values or even the existence (or non-existence) of values as true or false

  • undefined, null, false, 0, empty string, NaN can be interpreted as false
  • the existence of a non-false value can be interpreted as not-false, thus in many cases, true; Don't confuse not-false with true though. They are sometimes loosely interpreted as the same, but not-false contains everything that isn't interpreted as false, whereas true is much more specific.

Thus:

if (myMethod)
    myMethod();

Can be used to check for the existence of myMethod before running it.

The || symbol is a short-circuiting OR statement. If the first part of the OR is or can be interpreted as not-false, then that part of the statement will preside and the value will be taken and used as myValue. If javascript deems that the first part of the statement is interpreted as false, then the second part of the OR will be returned.

Thus in the statement:

var myValue = myInput.value || 0;

myValue will become whatever myInput.value contains if it contains anything that javascript can interpret as not false. Thus it could contain "Hi, hell of a day we've got here!" and that would be returned to myValue.

When I say not-false, I don't strictly mean true, because "Hi, hell of a day we've got here" isn't [strictly speaking] interpreted as true, but is "coerced" to being interpreted that way.

If myInput.value doesn't contain anything that javascript could coerce to being interpreted as true, then 0 will be returned to myValue.

So in this case, if myInput.value is undefined, null, false, 0 etc. then myValue = 0


It's a way of having a default value of 0 in case myInput.value is null (undefined)


Javascript has a defined left to right evaluation order. So if you write something like

if (method() || method2()) { ... }

method2() will never happen if method() returns true, because true OR anything will always be true.

This works the same for the example you are giving in Javascript.

If "myinput.value" evaluates to something trueish, this value will be assigned, otherwise 0 will be assigned for every falsish value (null, 0, "").


It sets myValue to the value of myInput or to 0, if the myInput value is anything that’s considered false in JavaScript (especially undefined or null). The trick is the short-circuit behaviour of the || operator. When you evaluate a||b and a is true, the || operator returns a:

console.log('a'||'b'); // 'a'

When the first argument is false, the || operator returns its second argument:

console.log(undefined||'b'); // 'b'

It’s also good to ponder this:

var foo = 0;
console.log('a'||foo++);
console.log(foo); // 0
console.log(undefined||foo++); // 0
console.log(foo); // 1


The || operator in many langiages includes an optimization called short-cutting: if the left side evaluates to a true value then the right side does not need to be evaluated. True || anything == true. This is often used to provide default values, as in your example code. If the left hand side myInput.value evaluates to true, then the whole expression will return that value. Otherwise, the whole expression will return the right hand value of 0.

Note that this also depends on the || operator returning the original values rather than a boolean true or false value. Each side is evaluated to true or false for the logic test, but not for the expression's return value.

function or(a, b) { if (a) { return a; } return b; }


MyValue will be equal to myInput.value OR 0 if the previous value can be regarded as False.

Value can be regarded as False is its empty string (''), zero (0) etc. Anything that say True in this code myInput.value == False


It means that if myInput.value isn't defined, myValue will be set to 0. Think || same as OR.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号