In javascript == vs =?

Sorry if this is posted a ton but search fields like to repurpose "=" so it's very hard to search for == javascript "== javascript" etc.

But I have some code I'm writing and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?

Example:

if ($('#block').css.display=='none') {
$('#block').css.display='block';

The only thing I can come up with is that in one I'm changing and in the other I'm checking. But in both I am referring to equality.

Answers:

Answer

= is the assignment operator. It sets a variable (the left hand side) to a value (the right hand side).

== is the comparison operator. It will tell you whether or not two values are equivalent regardless of the type of the operands.

=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.

I would checkout: http://www.codecademy.com/tracks/javascript for a quick intro to javascript.

If you prefer to read: https://developer.mozilla.org/en-US/docs/JavaScript/Guide is a great intro as well.

For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to be a source.

Answer

= assigns a value to a variable

== checks if the two parameter are equal to each other

=== checks if the two parameters are equal to each other and if their type is the same


! not operator

!= checks if the two parameters are not equal to each other

!== checks if the two parameters are not equal to each other or the type is not the same


one more

> checks if one parameter is greater than the other

>= checks if one parameter is greater than or equal to the other

>== DOESN'T EXIST


etcetera...

Answer

== is used to test if the value on the left is equal to the value on the right.

= is used to assign the value on the right to the variable on the left.

Answer

In javascript you have also the ===.

= This is for set the value to the variable.

== This is for compare if the value is the same.

=== This is for compare if the value is the same and also the type is the same.

Answer

The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.

Assignment Operators

Comparison Operators

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.