Web Analytics Made Easy -
StatCounter conditional syntax - CodingForum

Announcement

Collapse
No announcement yet.

conditional syntax

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      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!

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎