Search code examples
javascriptmethodsinclude

Output works and get "undefined" value too


I've this little code:

var mobili='mqihfdbacegklnprtuz&xyso';
function encrypt(index){
 if(mobili.includes(index)){
   var test = "It's working";
   console.log(test)
 }
 else{
   var test = "Bueller? Bueller?";
   console.log(test);
 }
}
console.log(encrypt('j'));

So... This code works but the output get to me the "undefined" value too. Why?

enter image description here

PS: I'm in repl.it website to run this one.


Solution

  • Your function isn't returning anything - it's actually calling console.log itself. You can fix this in two ways:

    One: Add a return statement to your function instead of using console.log:

    function encrypt(index){
     if(mobili.includes(index)){
       var test = "It's working";
     }
     else{
       var test = "Bueller? Bueller?";
     }
     return test;
    }
    

    Two: Call the function without using console.log:

    encrypt('j');