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))
{
t=t+1;
}
else
{
return (String.fromCharCode(t));
}
}
return (undefined);
}
console.log(fearNotLetter("abcdefghijklmnopqrstuvwxyz"));
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:
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.
Boolean("undefined")
// true
Boolean(undefined)
// 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?