[Twitch.tv Stream Checker] I keep getting "undefined" results with my javascript! What am I doing wrong?


I’m currently working on the Twitch.tv Stream Checker and I am unable to get the check online status functionality to work as it keeps on returning as undefined.

Here is my codepen: https://codepen.io/dopeland/pen/LjPRNZ

I know it has to do with returning the values, due to the correct data being displayed in the console, but when output it results as undefined. I feel like I run into this issue a lot so if anyone could provide any explanation of sorts that could help me not repeat this mistake in my code.


I’m not exactly sure what’s wrong with your code, but here’s a link to my project: https://codepen.io/TheyLeftMe4Dead/pen/NgdozM.
You’ll notice that I’ve broken down the steps into more generic methods. Also, it’s good to remember that ajax calls are Asynchronous, so you probably won’t get the data immediately. I hope looking at my project helps you out!


Unfortunately, it isn’t possible to return a value from an asynchronous function like you’re doing here in your ajax success function:

success: function(data){
  if (data.stream !== null) {
    status = data.stream.game;
    return status;
  } else {
    return status;

Instead, you will have to send the status to another function that writes it to the page, or simply write it to the page right there in your success function.