1

I want to make a program in javascript that yields "true" when two integers are equal and "false" when they are not.

I've tried using the "=" signs and the "===" sign, but I don't know why the results of program change depending on which sign I use, even without changing the numbers in the functions.

This code results in "false", which is what I want:

function checkEqual(a, b) {
  return (a === b ? true : false );
}

checkEqual(1, 2);

This code results in "true", which is NOT what I want:

function checkEqual(a, b) {
  return (a = b ? true : false );
}

checkEqual(1, 2);

The numbers in checkEqual() are the same in both attempts, but I want to know why the second one is "true".

LRitter
  • 149
  • 2
  • 11
  • 1 `=` sign means you are assigning a value from right variable to left variable. You should be asking `==` vs `===` – Void Spirit May 07 '19 at 17:45
  • single equal to `=` is setting the right variable as to left, this should be `Uncaught Syntax error` – Yash Karanke May 07 '19 at 17:45
  • I would like to add the I got the solution with only one equals sign from free code camp [here](https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/) which I thought was a legitimate source, so please excuse my faux pas. – LRitter May 07 '19 at 17:56
  • You missed the `warning` text that is in big font. – Bhojendra Rauniyar May 07 '19 at 17:58
  • FreeCodeCamp is a good source, but that happens to be a mistake. – Scott Marcus May 07 '19 at 20:13

4 Answers4

5

You're assigning a value with b value. Thus, a becomes true.

What you're trying to do is to check with equality operator ==.

  • = is assignment operator.
  • == is equality operator.
  • === is strict equality operator.

Take a look at the documentation to deep dive.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
4

= does not check equality, it is only used for assignment. The operators that check for equality in JavaScript are == (for "abstract" equality) and === (for "strict" equality). If a and b are both numbers, == and === will work the same.

2

First case (===):

function checkEqual(a, b) {
  return (a === b ? true : false );
}

checkEqual(1, 2);

Here you are using the identity operator (a.k.a. strict equality operator). It checks if the values are equal (and of the same type).

Since a is 1 and b is 2, then a === b becomes false (since they are different). And of course false ? true : false is false. So, the whole thing returns false. Yes, you might just do this:

function checkEqual(a, b) {
  return a === b;
}

checkEqual(1, 2);

Second case (=):

function checkEqual(a, b) {
  return (a = b ? true : false );
}

checkEqual(1, 2);

Here you are using the assigment operator. This is not an equality or identity check.

In this case, a = b assigns the value of b to a and returns it. So both a and b will be 2, and a = b evaluates to 2. Finally, since 2 is a truthy value, 2 ? true : false is true.

Note: that also means that if you were to pass a falsy value for b, you would get false. For example with the call: checkEqual(1, null); The value passed to a is irrelevant.


The case you didn't do (==):

function checkEqual(a, b) {
  return (a == b ? true : false );
}

checkEqual(1, 2);

Here you would be using the equality operator. It checks if the values are equal (however, it is free to convert the values to check).

Similarly to the first one, this will return false. What is the difference? Well, types. In Javascript "1" == 1 is true but "1" === 1 is false. That is, by using == you are allowing the runtime to convert the values in order to compare them, but with === you are not.

See JavaScript comparison operators: Identity vs. Equality and Which equals operator (== vs ===) should be used in JavaScript comparisons?

Theraot
  • 31,890
  • 5
  • 57
  • 86
0

In your second code snippet, you are using the assignment operator = rather than one of the comparison operators (== or ===), which is the main error here.

Now, to concisely answer your specific question, documentation reports that: "The assignment operation evaluates to the assigned value."

So, for example:

a = b ? true : false  --> if b != 0, this returns true,
                      --> if b = 0, this returns false
sal
  • 3,515
  • 1
  • 10
  • 21