Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning the higher value in logical operators in javascript [duplicate]

I am practising the logical operators in JavaScript and fully understood the concepts, but It seems that I didn't with this equation.

    const one = 1;
    const two = 5;
    console.log(one && two);

Why in this case it returns five and not one, shouldn't be returned the first value since both are true ?

like image 487
Joey Avatar asked Aug 26 '21 17:08

Joey


People also ask

What does double && mean in JavaScript?

JavaScript uses the double ampersand ( && ) to represent the logical AND operator. The following expression uses the && operator: let result = a && b; Code language: JavaScript (javascript) If a can be converted to true , the && operator returns the b ; otherwise, it returns the a .

Which operator returns true if all the conditions are true?

Logical Operators && is the logical and operator. It returns TRUE if both of the arguments evaluate to TRUE.

What is the output of a logical OR operation?

Remarks. The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .

What does the && operator do in JavaScript?

Logical AND ( && ) evaluates operands from left to right, returning immediately with the value of the first falsy operand it encounters; if all values are truthy, the value of the last operand is returned.

How many logical operators are there in JavaScript?

Logical operators There are four logical operators in JavaScript: || (OR), && (AND), !

What is the logical AND&&operator in JavaScript?

And the logical AND && operator is work with two or more operands. If a can be converted to true, the && operator returns the b; otherwise, it returns the a. In fact, this rule is applied to boolean values.

Does the logical AND operator return a Boolean?

But, like the logical or operator, the logical and operator doesn't necessarily return a boolean: Given a chain of multiple logical and operators a && b && c && d, JavaScript returns the left-most falsy value. View more jobs!

What is the result of the logical OR operator?

Their result can also be of any type. Let’s see the details. The “OR” operator is represented with two vertical line symbols: In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are true, it returns true, otherwise it returns false.


2 Answers

From MDN on the && operator:

"If expr1 can be converted to true, returns expr2; else, returns expr1."

So in this case, 1 can be converted to true, so it returns the second value, 5.

like image 176
bopbopbop Avatar answered Oct 23 '22 08:10

bopbopbop


The LOGICAL && operator returns the last value if all other values are true, or else it will return the first non truthy value.

i.e. Java != JavaScript

like image 36
BeerusDev Avatar answered Oct 23 '22 10:10

BeerusDev