Basic JavaScript: Use the Conditional (Ternary) Operator HELP ME

Basic JavaScript: Use the Conditional (Ternary) Operator HELP ME
0.0 0

#1

Hi

I am struck here, please help. What is wrong with my code?

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

checkEqual(1, 1);

THANKS IN ADVANCE!


#2

Your function does not return anything, so undefined gets returned by default.


#3

Hi randell,
I have typed return in front of true and false, as well. I still don’t pass it.

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

checkEqual(1, 1);


#4

Now you are not using the ternary operator as it is intended and creating an error.

You want to return the result of the ternary expression. Ternaries are not just another way to write an if statement which is what you are attempting to do with the last code you posted.

Think about how you would return a simple expression like a === b which would automatically evaluate to a true or false value.


#5

I think I’m struck. I’ll refer to some videos on YT! Thanks randell!


#6

The documentation for the ternary (conditional) operator gives great examples.


#7

thanks man, I finally got it! :slight_smile:


#8

I am stuck on this as well; my code looks like this

return (a === b) ? True : False;

I really don’t know what’s wrong here. How did you finally solve it?
Thanks for your help