Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assignments inside if-statement

Why can't I do this:

var fooElement, barElements;
if(fooElement = document.getElementById('foo') && barElements = fooElement.getElementsByTagName('bar') && barElements[0] && barElements[0].onclick)
{
    console.log(barElements[0].onclick);
}

This won't work either:

var foo, bar;
if(foo = true && bar = true)
{
    console.log('yay');
}
like image 763
Tyilo Avatar asked Aug 21 '11 17:08

Tyilo


People also ask

Can you do assignment in if statement?

Yes you can put assignment operator (=) inside if conditional statement(C/C++) and its boolean type will be always evaluated to true since it will generate side effect to variables inside in it.

What is multiple assignment statement?

multiple assignment A form of assignment statement in which the same value is given to two or more variables. For example, in Algol, a := b := c := 0. sets a, b, c to zero.

Is multiple assignment possible in C?

MultipleAssignment is a language property of being able to assign one value to more than one variables. It usually looks like the following: a = b = c = d = 0 // assigns all variables to 0.


1 Answers

Try this (it should give you a clue):

var foo, bar;
if((foo = true) && (bar = true))
{
    console.log('yay');
}
like image 73
Daniel O'Hara Avatar answered Sep 30 '22 10:09

Daniel O'Hara