2

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;
Community
  • 1
  • 1
Louis B.
  • 2,348
  • 1
  • 29
  • 51

9 Answers9

5

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
Naeem Sarfraz
  • 7,360
  • 5
  • 37
  • 63
  • -1. It sets `myValue` to `0` if `myInput.value` is a falsy value, which includes `undefined`, `null`, `0`, `false`, the empty string `""`, and others. Otherwise it sets `myValue` to the value of `myInput.value`. – Daniel Cassidy Sep 27 '10 at 13:45
  • This is also the same if `myInput.value = null` – BenAlabaster Sep 27 '10 at 13:45
  • *value* doesn't have to be undefined for the short-circuit or to kick in, it can be any of *undefined*, *NaN*, *null*, *0*, *false* or *""*. – Andy E Sep 27 '10 at 13:45
2

|| 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.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
1

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

BenAlabaster
  • 39,070
  • 21
  • 110
  • 151
  • This answer is a bit confused. All strings except the empty string coerce to `true`. The empty string coerces to `false`. Thus, `Boolean("Hi, hell of a day we've got here")` is `true`. – Daniel Cassidy Sep 27 '10 at 16:51
  • @Daniel Cassidy That was the point of what I wrote... coercing a value to true doesn't make it true. They're not one and the same. The value is not true but is coerced to being interpreted as such which was my point. It's not the same as `true`. I guess my wording isn't as clear as I thought. I was trying to remove the confusion that's caused by the difference between coersion and equals... if that makes sense. – BenAlabaster Sep 27 '10 at 17:02
  • Yeah, I know what you mean, and I’ve no doubt that you know what you mean :). The problem is that your use of “interpret” is a bit woolly. In one paragraph you say that “`"Hi, hell of a day we've got here"` can’t be interpreted as `true`” (meaning that it coerces to `true`, but that it isn’t the same as `true`, which is correct). On the next you say that “If `myInput.value` doesn't contain anything that javascript could interpret as `true`...” (meaning something that doesn’t coerce to true, which is the opposite to the way you used “interpret” in the previous paragraph). – Daniel Cassidy Sep 27 '10 at 17:10
  • @Daniel Cassidy Okay, I've changed my wording so that it's not as "woolly" ;) – BenAlabaster Sep 27 '10 at 17:20
0

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

yanjost
  • 5,223
  • 2
  • 25
  • 28
  • 2
    null and undefined are different, thus is null or undefined. The way your answer is worded implies that null and undefined are the same and they're not. – BenAlabaster Sep 27 '10 at 13:42
  • or an empty string "" or anything else that equals false in a == comparison. – Residuum Sep 27 '10 at 13:43
  • -1. It sets `myValue` to `0` if `myInput.value` is a falsy value, which includes `undefined`, `null`, `0`, `false`, the empty string `""`, and others. Otherwise it sets `myValue` to the value of `myInput.value`. – Daniel Cassidy Sep 27 '10 at 13:43
  • @Daniel Cassidy - Where did the term 'falsy' come from? I never heard that before. – BenAlabaster Sep 27 '10 at 13:47
  • Douglas Crockford and others use it as a shorthand for ‘values that coerce to boolean `false`’. – Daniel Cassidy Sep 27 '10 at 13:50
  • @BenAlabaster: http://11heavens.com/falsy-and-truthy-in-javascript – Skilldrick Sep 27 '10 at 13:53
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
zoul
  • 102,279
  • 44
  • 260
  • 354
  • -1. It sets `myValue` to `0` if `myInput.value` is a falsy value, which includes `undefined`, `null`, `0`, `false`, the empty string `""`, and others. Otherwise it sets `myValue` to the value of `myInput.value`. – Daniel Cassidy Sep 27 '10 at 13:46
  • That’s better. But StackOverflow won’t let me change my vote unless you edit again for some reason. – Daniel Cassidy Sep 27 '10 at 13:51
0

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, "").

fforw
  • 5,391
  • 1
  • 18
  • 17
  • While your statement is true, in this you can't assume the interpretation `var myValue` is boolean. In this instance javascript interprets the existence of myInput.Value and if no value exists then it's returning 0. – BenAlabaster Sep 27 '10 at 13:44
  • myValue will only be boolean if myinput.value is true. The interpretation of myinput.value will be regarded as true by the || operator according to the normal javascript rules that interpret null, 0, the empty string and NaN as false. – fforw Sep 27 '10 at 13:54
0

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; }

DougWebb
  • 680
  • 5
  • 11
0

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

NilColor
  • 3,462
  • 3
  • 30
  • 44
-1

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

  • -1. It sets `myValue` to `0` if `myInput.value` is a falsy value, which includes `undefined`, `null`, `0`, `false`, the empty string `""`, and others. Otherwise it sets `myValue` to the value of `myInput.value`. – Daniel Cassidy Sep 27 '10 at 13:47