0

I am a bit confused why the following two functions are equivalent.

//Function 1

function sumTo(n) {
   if (n === 0) return 0;  // base case
   return n + sumTo(n-1);  // inductive step
}

//Function 2
function sumTo(n) {
   return n ? n + sumTo(n-1) : 0;
}

In function 2, it uses the conditional operator where the condition is n. What does the condition n mean here? My major confusion is that n is not a boolean value, but why it can be evaluated as True or False? Even if it can, with some implicate type casting, where 0 can be interpreted as False, shouldn't it be return n ? 0 : n + sumTo(n-1) for the whole thing to make sense?
My guess is that n means n !== 0, but it looks so strange.

Ning
  • 499
  • 1
  • 4
  • 16
  • [What does an exclamation mark before a variable mean in JavaScript](https://stackoverflow.com/questions/19491491/what-does-an-exclamation-mark-before-a-variable-mean-in-javascript) – Spectric Aug 20 '21 at 14:05

4 Answers4

1

In Javascript every type of data can be evaluated as a boolean. There are two boolean values possibles: truthy and falsey values.

Some truthy values are:

  • Strings differents than ""
  • Numbers differents than 0
  • Boolean true

Some falsey values are:

  • Strings like ""
  • Number 0
  • Boolean false
  • Null and undefined values.

To convert a value into a boolean you can use the !! operator, known as the double not operator. It transform a value in true or false depends of its value (truthy or falsey).


My guess is that n means n !== 0, but it looks so strange.

You are right, when evaluating a value like n ?, without a comparison argument like n === "1" or n === "0" JS transform the value into a boolean in accordance of its value (truthy or falsey).

AlexSp3
  • 2,201
  • 2
  • 7
  • 24
0

This is the behaviour of JavaScript. n is evaluated here to be truthy. It'll be similar if you place n in an if statement as well.

From the MDN link on Truthy:

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).

Dan
  • 2,455
  • 3
  • 19
  • 53
0

As you suggested, by introducing any type of variable in a ternary operator, they are automatically converted to their boolean equivalent.

The two functions are not strictly equivalent because the first one explicitly checks if the variable equals 0, while the second one doesn't, so n will evaluate to false and return 0 if it contains any falsy value like false, '', undefined or whatever else.

function sumTo(n) {
   if (n === 0) return 0;  // base case
   return n + sumTo(n-1);  // inductive step
}

sumTo(false); // Uncaught RangeError: Maximum call stack size exceeded

function sumTo(n) {
   return n ? n + sumTo(n-1) : 0;
}

console.log(sumTo(false)); // 0
Guerric P
  • 30,447
  • 6
  • 48
  • 86
0

You are correct, in this case n means n !== 0, the same applies to empty strings for example var str = "", if you run if(str) will return false in this case, because the string is empty

Breno Prata
  • 712
  • 5
  • 10