typeof();

this javascript code:
 var weird = [1, 40, "bob", [], false, 89];
 var count = 0;
 for (var i = 0; i < weird.length; i++) {
  if (typeof(weird[i]) === "number") {
    count += weird[i];
  }
 }

number needs to be in quotes here or it doesn’t work.  That surprised me because I thought that would make it a string of the word number.    I tried putting
typeof(1);
into the console and it returned
“number”
So I guess that makes sense.  It just surprised me.  I guess because I’m asking to compare the typeof it would never return the value of a string, just what type.  It still seems strange that it should be in quotes but there you go!

Leave a Reply

Your email address will not be published. Required fields are marked *