1

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.

Pointy
  • 371,531
  • 55
  • 528
  • 584
o_O
  • 4,862
  • 11
  • 46
  • 84
  • 2
    = is assignment a = b means put b inside a. . == is unstrict equality, a==b means a is roughly equal to b . a===b is strict equality and the one you should be using – Benjamin Gruenbaum Aug 08 '12 at 19:20
  • 3
    Any JavaScript language guide will go into details. – Dave Newton Aug 08 '12 at 19:20
  • @RKS: Don't get the wrong idea, I'm trying to help and it's not "your fault" - it's hard to search for code syntax for sure. "Duplicate" does not mean "bad", hope the link helps. – Wesley Murch Aug 08 '12 at 19:26
  • 5
    This is not a duplicate of `==` vs `===`, he's asking about `=` vs `==`. – jbabey Aug 08 '12 at 19:26
  • 1
    @jbabey: You're right I didn't even notice since those two things are so completely different. – Wesley Murch Aug 08 '12 at 19:27
  • 1
    @WesleyMurch i never said the question made sense! :P – jbabey Aug 08 '12 at 19:28
  • But to be fair until I asked this I wasn't aware of ===, so now I am in need to know what's the diff so thanks for that link. – o_O Aug 08 '12 at 19:28
  • 2
    @RKS [This page](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators) covers JavaScript expressions in general. – Pointy Aug 08 '12 at 19:29
  • Related: https://stackoverflow.com/questions/2063480/the-3-different-equals – Jonas Wilms Mar 24 '18 at 21:22

5 Answers5

13

= 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.

Randall Hunt
  • 10,499
  • 5
  • 29
  • 39
  • 8
    AHHHH no w3schools! **Don't do it!!** – Naftali aka Neal Aug 08 '12 at 19:28
  • 2
    Both `==` and `===` check the types of the operands. It's what they may do with the result of those checks that differentiates them. –  Aug 08 '12 at 19:29
  • Ironically the w3fools.com website (the one where they are slamming w3schools) has a reference to = vs == in a section talking about how w3schools is wrong about it. – o_O Aug 08 '12 at 19:39
  • @jbabey Could you provide a source for "identity operator"? ECMA-262 refers to it as "The Strict Equals operator." – Dennis Aug 08 '12 at 20:00
  • 2
    @Dennis i think it may have been on MDN at one point in time. I'm not sure where it originated, but it is a fairly common phrase (google for "javascript identity operator") – jbabey Aug 08 '12 at 20:04
  • 1
    @Dennis ah, here we go. The very widely known book [JavaScript: The Definitive Guide](http://docstore.mik.ua/orelly/webprog/jscript/ch05_04.htm) uses the term "Identity operators". it probably originated here (or at least became much more common). – jbabey Aug 08 '12 at 20:07
5

= 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...

Community
  • 1
  • 1
Naftali aka Neal
  • 138,754
  • 36
  • 231
  • 295
4

== 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.

grdaneault
  • 790
  • 1
  • 10
  • 21
2

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.

Bruno Costa
  • 2,558
  • 2
  • 15
  • 24
2

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

kakridge
  • 1,923
  • 1
  • 16
  • 26