Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    conditional syntax

    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!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by tanker456 View Post

    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.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile conditional syntax

    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •