Difference between operators && e ||

Asked

Viewed 2,933 times

12

I’d like to know the difference between:

window.RM = window.RM || {};

And:

window.RM = window.RM && {};

Perhaps the examples are not correct, but I would like to know the difference between operators && and || in examples similar to those cited above.

Note: I already know how to use them in conditionals, in the context of logical operators, but I don’t know how to differentiate them in cases like the ones shown above.

  • You know the term falsy? Help these operators understand

  • I don’t know....

  • 1

    I’m sure this has already been answered, but I can’t research it now.

  • 1

    About falsy and Truthy: https://answall.com/q/271693/64969

  • I get it. But what does that have to do with my question?

4 answers

13


My searching for an answer to that question was fruitless, I should be using the wrong words. So, there goes an answer

These operators you refer to are Boolean operators. This means that they operate within the mathematical universe called "Boole algebra". This algebra differs from our traditional algebra because it does not work with numbers, but only with two values.

This algebra works on 3 axioms:

  • identity law (a == a at all times)
  • law of non-contradiction (nothing can be and not be at the same time)
  • law of the excluded third (either you are true or you are false, it cannot be something in between, a third value)

Read more here.

The operators used are the && "And" (and in English is less ambiguous) and the || "OR" (in English or).

They work in a certain way, which can be seen in truth tables. Note how the result depends on the first variable.

  • If the first variable is false, the "E" will be false as a whole; otherwise it will be the value of the second variable
  • If the first variable is false, the "OR" will have the value of the second variable; otherwise, just know that the first variable is true

So Javascript works like this for your boolean operators. Even, if he can already determine the value of the boolean expression by looking only at the first operand, he does not even go back to calculating the value of the second operand. The name of this strategy of not computing unnecessary things is "short circuit".

But in this case you’re not working with truth values. Or are you? See, in Javascript, they take it more lightly about the type of the object. You may not be dealing with booleans, but your values may be "true" or "Falsiformes" (not to be confused with "sickle cell").

A thing with "true form" or "true form" is called Truthy in Javascript and, for everything involving boolean expressions, are considered true. Already the ones with "false form" or "false form" are the falsy and are for all purposes equivalent to false boolean.

You can find more about these terms in the answers of this issue.

So, what’s going on?

In the case of "E", case window.RM be it Truthy, the expression will return {}, otherwise will return window.RM same. How this is being assigned in the variable window.RM, this seems to be a way to "zero" the object if it has some value.

In the case of "OR", it seems more like a kind of initialization of the object window.RM, for its value is only superscripted with {} in case he’s already falsy, otherwise (if Truthy) it does not have its altered value.

11

It’s called Short circuit evaluation.

When you use the operator && (AND), the value of the second argument will only be assumed if the first is true:

window.RM = window.RM && {};
// resultado: "undefined", porque window.RM não tem valor.

What it is to be true?

Other than empty, null, 0 and false.

window.RM = 0;
window.RM = window.RM && {};
// resultado: "0", porque window.RM é igual a zero.

Another example:

window.RM = '0';
window.RM = window.RM && {};
// resultado: "{}", porque window.RM é igual a zero, mas zero é uma string e não um número.

On the operator || (OR), is the opposite, where the first argument should be phony to assume the value of the second:

window.RM = 0;
window.RM = window.RM || {};
// resultado: "{}", porque window.RM é igual a zero.

Another example:

window.RM = window.RM || {};
// resultado: "{}", porque window.RM não possui valor, é "undefined".

And one more:

window.RM = true;
window.RM = window.RM || {};
// resultado: "true", porque window.RM é true, contrário de false.

In short, the && question for the argument on the left if it is true, if it answers yes, it takes the one on the right. Already the || question to the left if it is false, if you answer yes, take the right.

4

These operators are used in comparisons, the value on the right will be used:

  • (case ||) when the boolean value of the comparator on the left false
  • (case &&) when the boolean value of the comparator on the left true

One might also say:

  • (case ||) the first value that validates as true its Boolean value will be returned (evaluating from left to right), or false if both fail

  • (case &&) the value of the second operand (on the right) will be returned if the Boolean value of the first operand is true, even if the boolean value of the right operand is false, it will be returned. (i.e.: true && '' returns '' and not false)

In the specific case of the examples you show:

  • the || works as fallback, or if the value on the left does not exist (validate as false) the result of this assignment will be an empty object.

  • the && will be used and assigned to window.RM if the value to the left of the && work out how true. If the value on the left does not validate the assignment it will be with the value of the first operand (which has the Boolean value false).

Examples:

false || 25 // 25 (o primeiro falha, o segundo é usado pois valida como `true`, o valor final é 25)
true  || false // true (o primeiro é usado, o segundo é ignorado, o valor final é 25)
false || false // false (o primeiro falha, o segundo falha)

false && true // true (o primeiro falha, o segundo é ignorado pois o primeiro já falhou)
true  && 25 // 25 (o primeiro valida, o segundo é retornado, o valor final é 25)
true  && false // false (o primeiro passa, o segundo falha, o resultado é `false`)
null  && true // null (note-se que retorna `null` e não `false`)

0

Logical operators

The logical operators return true (V) or false (F) according to their operandos. ex1

Logical operators are also known as connective, for they are used to form new propositions from the junction of two others. To understand the operation of logical operators, we will resort to the our example of whole variables, To and B where A = 5 and B = 8. Table below displays examples of expressions using logical operators. ex2

You may have noticed from the previous examples:

  • when using the logical operator AND(&&), the result will only be true if the two related conditions are true;
  • for the operator OR(||), it is sufficient that one of the conditions is true for that the result is true;
  • as a consequence: with the operator OR(||), so that the result is false two conditions must be false.

Browser other questions tagged

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