Undefined in Javascript

return (undefined ) vs return (‘undefined’) in javascript

function fearNotLetter(str) {
var t=str.charCodeAt(0); 
//  console.log(t);
 for(var i=0;i<str.length;i++)
   if(t == str.charCodeAt(i))
      return (String.fromCharCode(t));
 return (undefined);

Your browser information:

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

Challenge: Missing letters

Link to the challenge:

what’s your question?

One is a string and the other is a primitive. You wouldn’t return the string undefined, but the type (normally).

If you were checking the typeof on some data you would check against the string.

let someVariable;
typeof someVariable === "undefined"
// true

It’s also worth pointing out that the string is a Truthy value and the primitive is Falsy.

// true

// false
1 Like

Also worth it to think of undefined as missing (a value missing to be set).
Take a sparse array for example let sa =[1, ,2] guess what is at index 1?