FAQ: Debugging JavaScript Code - Stack Overflow

This community-built FAQ covers the “Stack Overflow” exercise from the lesson “Debugging JavaScript Code”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Stack Overflow

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Using the ternary operator returns ‘undefined’…

const containsCake = (string) => {
  string.includes('cake') ? true : false
}

Does anyone know why?

you don’t return anything? The keyword is missing

3 Likes

Thanks! So just to understand how ternary operators work, this statement…

const containsCake = (string) => {
  return (string.includes('cake') ? true : false)
}

is the equivalent to…

const containsCake = (string) => {
  if (string.includes('cake')) {
    return true
    } else {
    return false
  }
}

So it looks like if/else statements return the result automatically, but ternary operators don’t?

2 Likes

The ternary operator is just a shorthand, it allows you to write a shorter condition.

no. I see return keywords in this code sample:

if (string.includes('cake')) {
    return true
    } else {
    return false
  }

if you do:

if (string.includes('cake')) {
    true
    } else {
    false
  }

you will also get undefined

the ternary operator does allow you to cut down the number of return keywords, you only have to use one return keyword vs 2 keyword with if/else.

JS does allow you to automatically return the result if the body of your function is only a single line:

const containsCake = (string) => string.includes('cake') ? true : false;

we could make this even smaller, if you only have a single parameter you can get rid of the brackets:

const containsCake = string => string.includes('cake') ? true : false;

furthermore, the includes method itself already returns a Boolean value, thus we can even do:

const containsCake = string => string.includes('cake');

but i suppose that beats the point of teaching the ternary operator.

why does your function have a parameter? You don’t even use it:

const containsCake = () => string.includes('cake');
1 Like

Super helpful!

I see I was mixing it up with the single line version. It’s cool that it can be shortened even further, and technically doesn’t need the ternary operator…

Good stuff, thanks!

Regarding your question, the (string) parameter was already included in the function declaration. According to the lesson instructions, the purpose of the containsCake function is to take a string and check if that string contains the substring 'cake' inside of it.

I tried omitting the (string) parameter, like in your last example, but it throws back an error.

string is needed, sorry, my mistake. you check if cake is included in string

you’re welcome :slight_smile:

i considered that possibility, which is why i showed the shorthand.

1 Like

The if statement is exactly how I did it, because the problem hinted towards using ‘if’. :slight_smile:

While the easiest answer is to use the “contains” method, the Stack Overflow article and responses clearly shows that it does not work on Internet Explorer or other older ES5 browsers. This does not make a huge difference these days, but there are people out there who use older systems, so I would suggest that using “indexOf” would be a more complete solution. That, or using a polyfill…