Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  20] [ 2]  / answers: 1 / hits: 5500  / 10 Years ago, tue, april 8, 2014, 12:00:00

How does the below code execute?



if(a=2 && (b=8))
{
console.log(a)
}


OUTPUT



a=8

More From » operators

 Answers
9

It has nothing to do with the if statement, but:



if(a=2 && (b=8))


Here the last one, (b=8), actually returns 8 as assigning always returns the assigned value, so it's the same as writing



a = 2 && 8;


And 2 && 8 returns 8, as 2 is truthy, so it's the same as writing a = 8.


[#46207] Monday, April 7, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;