-4

I read the following code but I can not understand what it means "||" in this context:

function factorial(numero) {
  numero = numero || 1
  return numero * factorial(numero - 1)
}

I understand the logical operators but I do not find the sense to call the function if you pass any argument. That is why the reason for my question.

Iván
  • 401
  • 2
  • 6
  • 17
  • 2
    Possible duplicate of [How does javascript logical assignment work?](https://stackoverflow.com/questions/4446433/how-does-javascript-logical-assignment-work) – Ivar Apr 14 '18 at 22:16
  • Or [JavaScript OR (||) variable assignment explanation](https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation) – Ivar Apr 14 '18 at 22:17
  • I understand the logical operators but, after your answers, I do not find the sense to call the function if you pass any argument – Iván Apr 14 '18 at 22:23
  • It's the same as having a default value. Here, you could just call factorial() without passing in anything, and you know 'numero' will be set to 1 anyway. – Dan Oswalt Apr 14 '18 at 23:13
  • Maybe not that useful here, but it's like saying it's an optional argument. – Dan Oswalt Apr 14 '18 at 23:19

5 Answers5

5

That's called short-circuiting. || is the OR operator, but the way it is evaluated, it will look at the left side (and never look at the right side, thus "short-circuiting".

If it is true, it will use that value. If it is false, it will use the right side. Here, if 'numero' is undefined, it will be false, and therefore the placeholder default value of 1 will be used.

Dan Oswalt
  • 2,201
  • 2
  • 14
  • 24
3

It is like a fallback. If numero is a falsy value (false, '', undefined, null, NaN, 0) it will set the value to 1.

As we can see here in these two tests, if a values is not passed it will use the fallback otherwise it will use the value passed as a parameter.

function test(value) {
  console.log(value || 'Not Set')
}

test()
test('Awesome')

There are also more advanced ways which work differently but also produce the similar effect. Here we can do the complete opposite by using && instead which will only run the next item if the previous command is truthy.

let items = []

function addItem(a) {
  let contains = items.includes(a)
  !contains && items.push(a)
}

addItem('cat')
addItem('dog')
addItem('pig')
addItem('cat')
addItem('cat')

console.log(items)

In the above we use && instead which will do the exact opposite of ||, so if contains is true, we run the next command, otherwise end the current statement.

Lastly, we can combine the two and get a whole new result:

let items = []

function addItem(a) {
  let contains = items.includes(a)
  !contains && a % 2 == 0 && items.push(a) || console.log('Item', a, 'is not valid')
}

addItem(1)
addItem(2)
addItem(10)
addItem(15)
addItem(10)
addItem(100)
addItem(102)
addItem(103)

console.log(items)

And with this example we only insert items into the array if they are not already in the array and are an even number. otherwise we will output that the value isn't a valid insert either because it was already in the array, or it wasn't event.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
1

It means or it always means or in JavaScript. What that statement does is to set numero to the current value of numero if it is a truthy value or 1 if it is not.

The new syntax, allows you to do this:

function factorial(numero = 1) {
    ....
}

Which is more convenient for setting default values to parameters.

Titus
  • 22,031
  • 1
  • 23
  • 33
0

It only expands the second branch If In your case parameter is not given , as mentioned above It is called Short circuit evaluations.

Treat it like IF numero was passed assign it to variable ELSE set variable to 1

NarrowVision
  • 108
  • 7
0

In Javascript, but also in other languages, the operator || is defined as:

Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true. (https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Operators/Logical_Operators)

That snippet is a shorthand for:

if (!numero)
    numero = 1;

So, if numero is a falsey value (0, Nan, null, undefined or an empty string) it will be set at 1.