Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 2 && 3 results in 3 (javascript)? [duplicate]

When I type in browser's console:

console.log(2 && 3)

it results always with second number (in this case 3):

3

Can someone explain me why?

like image 506
lukaszkups Avatar asked May 15 '15 06:05

lukaszkups


People also ask

Why does 2 mod 4 = 2?

Because 2 = 0 * 4 + 2. In x/y results consists of an integer part and a fraction part. If you multiply the fraction part with the divisor, you get the remainder. And x = Integer party + Remainder (i.e. Fraction party). In this case Integer part is 0, and the remainder is 2. glad you had the courage to ask that question.

What does'2 mod 3'mean?

How many ever '2 mod 3' I put in between, it is printing 2 as the answer. Please can anyone explain this behavior Show activity on this post. I don't think you completely understand modulus. The '%' symbol reads mod or modulus. Essentially 2 mod 3 = 0 with a remainder of 2. The remainder of 2 is your answer. 2 mod or % 3 = 2.

Why is 2 modulo 4 2?

That's why 2 modulo 4 is 2. The modulo operator evaluates to the remainder of the division of the two integer operands. Here are a few examples: mod means the reaminder when divided by. So 2 divided by 4 is 0 with 2 remaining. Therefore 2 mod 4 is 2. Modulo is the remainder, expressed as an integer, of a mathematical division expression.

What is 1x2 times 0?

2) by a positive exponent number of times. Or 1 is divided by a base number by a negative exponent number of times. Therefore signifies 1 x 2 (one time), is 1 x 2 x 2 (two times), and is 1 times no twos (not times zero, just nothing). 1 times 0 is 0, 1 times nothing is still just 1.


1 Answers

If the left hand side of && evaluates as a false value, the whole expression evaluates as the left hand side.

Otherwise it evaluates as the right hand side.

2 is a true value, so 2 && 3 is 3.

For comparison, try console.log(0 && 1) and console.log(false && "something").

like image 186
Quentin Avatar answered Oct 17 '22 08:10

Quentin