-5
var hungry = true;
var foodHere = true;

var eat = function() {
  if (hungry && foodHere === true) {
      return(true);
  } else {
      return(false);
  }`

};

The first line is the correct syntax. For a long time I was just saying hungry && foodHere = true... and I couldn't figure out (and still don't understand) why that is wrong. I understand the difference between = (assignment) and === (equal to). I assigned the variables to be true initially, so aren't I asking in the if statement if that's what they're set to? Why am I setting the variables = to in the var definition, but then when checking them I'm using the === value?

Travis
  • 1,274
  • 1
  • 16
  • 33
Jeremy H
  • 17
  • 3
  • Read http://stackoverflow.com/questions/523643/difference-between-and-in-javascript – Mate Feb 21 '15 at 02:54
  • 2
    _“Why am I setting the variables = to in the var definition, but then when checking them I'm using the === value?”_ – because the first one is an assigment, and the second one a comparison …? – CBroe Feb 21 '15 at 02:55
  • your `hungry && foodHere === true` syntax is wrong. it's not doing what you want it to do. It should be: `hungry === true && foodHere === true`. if you want to assign 2 variables at the same time you would do `var hungry = true, foodHere = true`. You can't shorthand assign and you can't shorthand compare either. – Ryan Mar 16 '15 at 01:26
  • Why does this question have 5 downvotes but no votes to close? The downvotes don't seem helpful. – jcollum Jan 13 '16 at 20:30

2 Answers2

5

= is only used to assign variables. === or == are used to compare. For a good example, we must look into comparison operators.

Syntax

The syntax of comparison operators is fairly simple, use them to evaluate expressions. The comparasin operators are:

=== //strict equality
==  //Parsed or partial equality
>   //Greater Than
<   //Less than
>=  //Greater than or equal to
<=  //Less than or equal to

To properly use these, you must know the proper syntax. For example, I can't do something like:

if(true == 1 === true) //do something

as that would invalidate the code, and slow it down by much using ==, which brings me to my next section.

Equality

The two equality operators in JavaScript are == and ===. They do two very different things.

===

The strict equality (===) tests whether two values are exactly equivalent, both in type and value.

==

The Parsed equality (==) tests whether two values are equalivent in value, but parses to try and connect different types.

Inequality

There are 2 main inequality value in JavaScript (!==) they are pretty self explainatory based on the equalities (===, and ==)
here's a chart explaining the three.

          1      0      true      false      null      undefined     ""
        1 ===   !==       ==        !==        !==         !==      !==
        0 !==   ===      !==        ==         ==          ==       !==
     true  ==   !==      ===        !==        !==         !==      !==
    false !==   ==       !==        ===        ==          ==       ==
     null !==   ==       !==        ==         ==          ==       ==
undefined !==   ==       !==        ==         ==          ===      !==
      ""  !==   ==       !==        ==         ==          !==      ===

Adding onto what @jcollum said, = defines a variable value, and if(something === true) simplifies into if(something). Similarly, if(something === false) simplifies into if(!something).

You also need to do comparisons separately. if(7 & 6 < 10) returns false because it is the simplified version of if(7 === true && 6 < 10).

Travis
  • 1,274
  • 1
  • 16
  • 33
1

It turns:

hungry && foodHere === true

into

hungry && true

or just

hungry

Using the assignment operator instead of the comparison operator is stopping your logic from working correctly.

jcollum
  • 43,623
  • 55
  • 191
  • 321