...

View Full Version : conditional syntax



tanker456
09-07-2011, 10:13 PM
I just found this forum! I'm glad because I wanted to ask some javascript questions before.

There was a question at work before at why one would use the following:

if (a=='1') ...

or

if ('1'==a) ...

Is there any kind of advantage of using one of the other?

Thanks for any response!

DaveyErwin
09-07-2011, 10:26 PM
if (a=='1') ...

or

if ('1'==a) ...

Is there any kind of advantage of using one of the other?

Thanks for any response!

Yes there is if you are prone to typos
like me. if you accidently write
if (a='1') ...
instead of
if (a=='1') ...
the condition will always be true,
and it may be hard to find such a "bug".

If you accidently weite
if ('1'=a) ...
instead of
if ('1'==a) ...
You will get an illegal assignment error
and the problem will easily be found.

tanker456
09-07-2011, 10:31 PM
This is exactly what we thought also, if you use the assignment instead of the conditional statement.

We were wondering because in some sample code, we did see it coded with the literal string on the left side of the conditional equal symbol.

We have looked all over the internet, but saw no other reason why you would code this way.

Thanks for your response!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum