2

I have read many javascript tutorials and I believe i know many things until now. So i thought that it will be a good idea to see a few script written in this language. My problem is that i don't know what this line really does:

var b=Number(ls.autofarm)||0,g,h,i,d,j; 

I know the basics of these line, that the variable name is b and it has the value of Number(Is.autofarm)||0,g,h,i,j, but why are the "||" are using? It will be great if someone could explain to me what this line does.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • 1
    What makes you believe that is the correct value for b? – Anthony Forloney May 02 '13 at 01:29
  • 1
    I suggest spending some time getting an understanding of the [logical operators](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators#Logical_operators). – Marty May 02 '13 at 01:30
  • possible duplicate of [What does "options = options || {}" mean in Javascript?](http://stackoverflow.com/questions/2851404/what-does-options-options-mean-in-javascript) and potentially [many, many, many others](http://stackoverflow.com/search?q=[javascript]+%22||%22). – Felix Kling May 02 '13 at 02:47

5 Answers5

3

|| in javascript is often used as a way to supply a default value. The first non-false value, or the last value found, "wins".

e.g.

var a = "foo";
var b = null;
var c = a || 2;   // c == "foo". a was non-false, so we use that
var d = b || 2;   // d == 2.  b was false (null, 0, "" are all false), so we moved along

in your case,

// b == Number(ls.autofarm), unless that's false/null/undefined. Then we use 0
var b = Number(ls.autofarm) || 0,
g,h,i,d,j;      // a bunch of other variables
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 2
    Better to use the term "falsey" or something instead of "false" since technically `null` and some other values are not `false` but are "falsey". –  May 02 '13 at 01:35
  • Your answer is wrong. The variable `b` results in `j` if `Number(ls.autofarm)` is falsy. This is because the comma operator always returns the __last__ expression (i.e. `j` not `0`). Change that and I'll remove the downvote. – Aadit M Shah May 02 '13 at 01:46
  • 2
    No, in this case the comma terminates a variable definition. From the Chrome console: > j = "j was here" "j was here" > ls = { 'autofarm': false } Object {autofarm: false} > var b=Number(ls.autofarm)||0,g,h,i,d,j; undefined > b 0 > j "j was here" – Paul Roub May 02 '13 at 01:52
  • After some more thinking I realized that the variable `b` always results in `j`. This is because the comma operator always returns the __last__ expression (i.e. `j`) and has the lowest precedence. – Aadit M Shah May 02 '13 at 01:53
  • Again, j isn't a value involved in b's initialization. It's the name of another variable being declared. – Paul Roub May 02 '13 at 01:54
2

The line

var b=Number(ls.autofarm)||0,g,h,i,d,j; 

Can be split into two parts if it helps you understand.

var b = Number(ls.autofarm)||0;
var g,h,i,d,j; 

The second line is a declaration of empty variables, while the first line is declaring b with a value. If the value to the left of the || is falsey, the value to the right is used. In this case, if Number(ls.autofarm) is false, b will be 0.

James Bruckner
  • 909
  • 7
  • 10
  • 1
    This is a good example of where the original snippet is a case of optimization over maintainability. I'm hoping that it is from some minimized script! – Jon P May 02 '13 at 01:37
  • For sure, it's a good point to also remind people new to programming to *please* don't write code like this. :D Although, it's a cool exercise in learning the rules of how the syntax works. – jamesmortensen May 02 '13 at 01:59
1

var b=Number(ls.autofarm)||0,g,h,i,d,j;

Seems to me that b depends on the value of ls.autofarm OR is simply 0 (zero).

The other g,h,i,d,j; are decelerations of variables similar to

var b=Number(ls.autofarm)||0;
var g;
var h;
var i;
var d;
var j;
Daniel
  • 4,816
  • 3
  • 27
  • 31
0

This is the relevant part (the other letters are just variable declarations):

var b=Number(ls.autofarm)||0

If Number(ls.autofarm) evaluates to true then it will be assigned to b. If it evaluates to false then 0 will be assigned to b instead.

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0

|| is the logical OR operator, wich is also a falsey coalscing operator in JavaScript, wich means that it evaluates to the left-side of the expression if the value is not falsey, otherwise it evaluates to the right-side value.

Falsey values are: false, empty string '', 0, null, undefined and NaN

The || is often used to supply default values.

function (someArg) {
    someArg = someArg || 'test'; // 'test' if someArg is falsey
}

When there are multiple || operators, here's how it evaluates:

var a = 0 || '' || true || false;
  1. It first evaluates 0 || '', wich returns ''
  2. Then it evaluates '' || true, wich returns true
  3. At this point, the expression will not be further evaluated and true will be returned.

Note that multiple variables can be declared and initialized with a single var statement, like in your example.

var b = Number(ls.autofarm) || 0, g, h, i, d, j;

The above initializes b to Number(ls.autofarm) || 0 and declares the g, h, i, d, j variables.

plalx
  • 42,889
  • 6
  • 74
  • 90