What use ==?

D

Dan

Hi,

Why would you use = = instead of =?

What's the difference between ++J and J++?

Many thanks
 
L

Lee

Dan said:
Hi,

Why would you use = = instead of =?

What's the difference between ++J and J++?

You should find a book or tutorial site.

You would never use = =
but you use == as the comparison operator for equality:
if(alpha==beta)
The single = is the assignment operator.
alpha=1;

alpha=J++
sets alpha to the current value of J and then adds 1 to J.

alpha=++J
adds 1 to J and then sets alpha to the current value of J.
 
M

Martin Honnen

Dan said:
Why would you use = = instead of =?

The operator = is the assignment operator, the operator == is a
comparison operator that does some type conversion on its arguments.
Thus if you want to compare things you use ==
expression1 == expression2
if you want to assign to a variable or property
variable = expression
 
E

Evertjan.

Dan wrote on 11 nov 2003 in comp.lang.javascript:

Try this:

<script>

// > Why would you use = = instead of =?


// = is the asignment operator

a=4
alert(a=7) // 7
alert(a) // 7

// == is logical compare

a="7"
alert(a==7) // true

// === is strict logical compare, without type conversion.

a="7"
alert(a===7) // false


// > What's the difference between ++J and J++?

// ++J increment operator before evaluation
// ++J increment operator after evaluation

j=3

alert(j) // 3


alert(j++) // 3

alert(j) // 4

alert(j++) // 4

alert(j) // 5

</script>
 
M

Michael Winter

on 11/11/2003:
Hi,

Why would you use = = instead of =?

What's the difference between ++J and J++?

Many thanks

You do realise that these are very basic concepts within the language,
don't you? I suggest you read the Client-side JavaScript Guide.

== and = are *entirely* different operators. There's no 'instead of'
about it: they're not even related.

== is a comparison operator that return a boolean. If the right
argument evalutes to be the same as the left argument, true is
returned, false otherwise.

= is the assignment operator. It stores the result of the expression
on the right to the variable indicated on the left.

++var is called the prefix increment (pre-increment) operator. var++
is the postfix increment (post-increment) operator. The difference
between them is when the increment occurs:

a = 5;
myFunc( a++ ); // 5 is passed to myFunc
// 'a' contains 6 here

a = 5;
myFunc( ++a ); // 'a' contains 6 here and myFunc receives 6

The second example is effectively the same as:

a = 5;
a++;
myFunc( a );

Mike
 
D

Dan

Thank you to all that answered. I did realise that the code should have
been == but was unsure if this would be recognised so used = = (should have
known better).

I have been going through some introduction books to JavaScript but they
just used == in an example without actually explaining what this did (the
chapter was covering if, switch and for) so I thought I could probably get a
good explanation here.

The book also tried to explain the ++var and var++ but I couldn't get my
head around it fully but your explanations have proven very helpful.

Thanks again, and hopefully soon, after a lot of reading and learning I can
have a better question for you and you never know even answer one myself.
 
D

Douglas Crockford

Thank you to all that answered. I did realise that the code should have
been == but was unsure if this would be recognised so used = = (should have
known better).

I have been going through some introduction books to JavaScript but they
just used == in an example without actually explaining what this did (the
chapter was covering if, switch and for) so I thought I could probably get a
good explanation here.

The book also tried to explain the ++var and var++ but I couldn't get my
head around it fully but your explanations have proven very helpful.

Thanks again, and hopefully soon, after a lot of reading and learning I can
have a better question for you and you never know even answer one myself.

There are tons of really bad JavaScript books. It looks like you have one. Toss
it in the landfill immediately and go out and get a good one, like Flanagan's
from O'Reilly.

http://www.crockford.com/javascript/javascript.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top