The && 'operator in Javascript vs in Java

I am currently reading JavaScript from JavaScript: The Definitive Guide. On page 76 this expression is written,

o && ox // => 1: o is truthy, so return value of ox 

As a Java programmer, I want to ask why it returns 1 instead of 'true' In Java, this was not so, but in any case, I know that JavaScript is different, but the logical AND mechanism is the same everywhere. (In C, it returns 1 as true.)

I ask why this makes sense for this behavior in general?
Is there any way I can guarantee the return of only true or false values?

+6
source share
6 answers

According to the specification for binary logical operators

The value generated by && or || an operator does not necessarily have a Boolean type. The value produced will always be the value of one of the two operand expressions.

This is a function used in javascript a lot, one common use case is to assign a default value to a variable if it is not defined. Suppose you expect an options object as a parameter, but it is not necessary that the user cannot pass it.

 function x(options){ options = options || {}; //now your can access optionx.a without fearing whether options is undefined } 

You can do something like !!(o && ox) to always get true / false

+7
source

if we have:

 expr1 && expr2 

& & Returns 'expr1' if it can be converted to false; otherwise returns 'expr2'. Thus, when used with boolean values, && returns true if both operands are true; otherwise returns false.

So, if we have:

Var xyz = a && b; // where a is undefined (false) and b is 123, then it will highlight undefined.

In the case of if statements, these values ​​are specifically converted to boolean.

Refer to the following link: " https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators "

+4
source

You can use Boolean(o && ox); to get true or false

+2
source

Returns the value of the correct operand. If ox was true , it would return true . If ox was 'banana' , it would return 'banana' .

 var o = {x:true}; console.log(o && ox); 
+1
source

Technically, the && operator is implemented to return the first operand, if it is not equal to zero, and returns the second operand, otherwise in many languages, including Java, C / C ++, Javascript, Perl ... Although most languages ​​accept any type of operands, Java forces you to use logical operands so that it always returns true or false. To force a boolean result to use Javascript:

 Boolean(a && b) 
+1
source

This behavior is called coercion . Coercion is an action that causes an object to behave like a different type, and logical operators can produce coercion when trying to access the value of an object for evaluation.

It is important to remember the table for truth and false values, because due to coercion different results can be obtained.

  • false creates false
  • 0 creates false
  • "" creates false
  • NaN creates false
  • null creates false
  • undefined creates false

Everything else creates true , including the text "0" and "false" , functions, arrays, and empty objects.

Given the rules for logical operators, there is a short circuit assessment in JavaScript, for example:

 0 && 1; //-> 0, falsy value !(0 && 1); //-> true, falsy value negated !!(0 && 1); //-> false, boolean falsy value void 0 && alert("1") //-> undefined (void produces undefined) [] && Math.ceil(9.1) //-> 10, truthy value {} && someFunc() //-> the result of someFunc() 0 || null; //-> null, falsy value null || "0"; //-> "0", truthy value "" || void 1; //-> undefined, falsy value !!(+"5px" || {}); //-> true, boolean truthy value 

Coercion is useful when you must check the defaults to prevent errors, for example.

 function divide (a, b) { a = +a; //-> coerced to number b = +b; //-> coerced to number if (!a) return 0; //-> NaN or zero return b && a / b; //-> b must be a number different from zero } divide(); //-> 0 divide(5); //-> NaN, no error thrown! divide(5, "a"); //-> NaN, no error thrown! divide(5, 0); //-> 0, division by zero prevented! divide(49, 6); //-> 8.1666 

If you want to prevent NaN from returning, just add another constraint to the return statement:

 return (b && a / b) || 0; 

You can check other coercion cases: Coercion in JavaScript


Happy coding!

+1
source

Source: https://habr.com/ru/post/988892/


All Articles