Use the Conditional (Ternary) Operator: != vs =

Tell us what’s happening:
So, the following code does not work:

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

checkEqual(1, 2);

but the solution below does, and I’m not sure why…
Your code so far

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

checkEqual(1, 2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge:

to check for equality you need to use == and not =

Ok, but the question was about != vs = …

So was the answer…

== means equal to
!= means not equal to
= means assign the value of the right operand to the left operand

The documentation I linked should make this clear.

= is not used for comparing values at all.

(a == b ? true : false) will give the same result as (a != b ? false : true)

If you want to know what is happening here in your example:

(a = b ? true : false)

The value of b is assigned to a and then a is evaluated to see if it is truthy or falsy.

1 Like

I think it could be the problem of braces.
You should write as below:

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

checkEqual(1, 2);

Please don’t use ==. Always use ===.

Also you don’t need ternary for this example.

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

But if you want to use them to see how they would look, I highly recommend this stylistic approach:

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

Again., it’s more natural to read non-negative so I would change !== to === and use true first.

I think you missed that that was the point.
Also, the point was that I wanted a == and not a =, I didn’t want to use a === because i want a value comparison.

The answer was that I needed to use a value comparison of == and not a declaration of =.

I didn’t miss any points. I think you did. Always use ===.

Using == gives unexpected results.

Your example does this:
checkEqual(1, 'Some_Stupid_String'); // true

My example does this:
checkEqual(1, 'Some_Stupid_String'); // false

And = does not mean declarative. It means assignment.