Question:

Single '=' and double '=='?

by  |  earlier

0 LIKES UnLike

i'm doing some programming..

i know single '=' means we assign value into a variable..

and double '==' means equal....

but i am confused to compare those..

let say, i have a 'login.html' + javaScript page with 2 textfield..

which is username and password...

i want to compare if those textfield are not empty..

how should i do?

either

======================================...

if((username = "") && (password = ""))

======================================...

or

======================================...

if((username == "") && (password == ""))

======================================...

please help me!!

thanx in advance =)

 Tags:

   Report

6 ANSWERS


  1. If you want to compare anything then use == and if you want to asign something to variable then use = sign

    http://milindkansagara1984.blogspot.com


  2. I will try as non-technical as a can, to describe this:

    The difference is like remembering that its raining outside vs asking if it is raining outside... make sense?

    = will STORE data FROM the right side INTO the variable on the left side of the expression.

    == will COMPARE the data FROM the right side TO the data on the left side of the expression and EVALUATE the expression to True OR False.

  3. use == (double equal sign)

    single "=" is an assignment operator

    double "=" is a comparison operator

  4. An 'if' statement needs to evaluate to a boolean value (i.e. true or false). This implies "==" be used, which can only evaluate to a boolean result. The problem is that many early languages allowed a zero value of an expression be evaluated as 'false' and any nonzero as 'true'. This is why using "=" in the 'if' doesn't cause a syntax error in C and other early languages. More recent languages (C#, for example) are stricter in their variable typing and eliminate the problem.

    The best way to avoid the problem (other than a strict-typing language) is never use the shortcut and instead always explicitly check a boolean condition: "If 'if', equals-equals".

  5. well,

    "=" makes "a" equivalent to "b"

    "==" checks if a is equivalent to b,

    so if you put a while(a=b) than while(a==b), then it'll be an infinite while loop until b becomes zero, w/c is pretty weird

  6. "=" is = "equal to" but (matches similiar string)

    "==" is = "Exactly equal to" (matches exact string

    see this for more clear http://developerskb.blogspot.com

Question Stats

Latest activity: earlier.
This question has 6 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.