Shouldn’t this function return a bool instead of a pizza?

Asked

Viewed 69 times

4

I am studying Act and I came across the following expression:

{this.state.text.split(' ').map((word) => word && '').join(' ')}

This summary expression will print a pizza for each word inserted in the text:

Follow the example:

https://facebook.github.io/react-native/docs/handling-text-input

Well the doubt is not about Act but about what will be returned in this function:

(word) => word && ''

This function receives a word word and if the word is valid then a pizza will be returned

But in case I do this:

(word) => '' && word

Word will be returned and not a pizza.

But shouldn’t this condition return a bool, since it is a boolean condition? And why always the last clause that is returned? Or this has to do with the Map function?

3 answers

5


The operator && in Javascript evaluates the first expression and being it something that can be true, returns the value of the second expression.

For example:

console.log('a' && 'b' && 'c');

The values 0.0 are considered false, null, undefined, false, '' and NaN. The rest is regarded as true.

  • So it’s some kind of recursion? In the case of if it checks the first clause, if it is true it passes to the next one, but as there is no if and not a while then it returns the clause instead of a bool?

  • 1

    @Viniciusmorais more or less that. It uses the value of the last clause as if it were a Boolean, even if it is not. There is an analogous behavior with the || also.

3

This behavior is called Short-Circuit Evaluation or Short Circuit Evaluation (Wikipedia and MDN), which basically means: "If I already know the answer, I won’t even check ahead".

Take as an example the truth tables of an AND and an OR:

+---------+-------+-------+  +---------+-------+-------+
|           AND           |  |            OR           |
+---------+-------+-------+  +---------+-------+-------+
|     entrada     | saída |  |     entrada     | saída |
+---------+-------+-------+  +---------+-------+-------+
| false   | false | false |  | false   | false | false |
| false   | true  | false |  | false   | true  | true  |
| true    | false | false |  | true    | false | true  |
| true    | true  | true  |  | true    | true  | true  |
+---------+-------+-------+  +---------+-------+-------+

We can understand that the AND will only be true if ALL your entries are true, and the OR will only be false if ALL your entries are false. So the interpreter does not need to know what comes after a false && ... because it is already known that the result is false.

So when do we have:

obj.attr && doSomething(obj.attr)

It will only perform the function doSomething if obj.attr for some value Truthy.

As well as:

let valor = param || default;

The variable valor will receive the first value Truthy that she finds, from left to right. If she does not find any, it will be the last value (in this case default);

2

The function map takes the array values and uses them as the input key (any word typed) and maps them all to the same value (the pizza).

The end ('' && word) serves to print nothing when the input key of the map is an empty string. If word there is a value (because the pizza always has a value), will be assigned the value of the last variable evaluated (in the case of this phrase, word). If the pizza comes last, it’s her who goes.

Is a "short circuit"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.