In javascript can I use something like if( val = sum(2,3))?
In开发者_高级运维 JavaScript when is it correct and cross-browser to use.
function sum(v1,v2) { return v1+v2;}
...
if(val = sum(1,2)) alert(val);
else alert('result zero or null');
...
Of course you can. It works in many(or some - at least it doesn't work in Python) program languages like PHP.
That's why many programming beginners have trouble with =
and ==
.
Yes. Yes. Yes. Yes. Yes. Yes. Yes. Yes. OK. I've reached the character minimum.
But make sure that you do double equals signs: if ( x == y)....
I believe there was one version of JavaScript (1.2 perhaps?) where if (x = y)
would be corrected to if (x == y)
.
精彩评论