typeof - some unexpected results

32 views
Skip to first unread message

Inger Hohler

unread,
Aug 24, 2018, 11:40:24 AM8/24/18
to Khan Academy Javascript Technical Q&A
I am trying to explore 'return', closure etc. and wanted to try to find examples of returning different data types.
I tried to follow the list here https://www.w3schools.com/js/js_datatypes.asp and found some unexpected results, the code which causing issues is on a yellow background.

Are there anyone here who can help explain what is happening? Or if it's a good idea that BabyHints reacts to what I place in println brackets? I think one of the things I saw is related to https://github.com/Khan/live-editor/issues/553 but I don't know if it's a different thing than those already reported, and should be added to the list.

primitive data types
*string 
println(typeof("hope & pray")); returns string
I can use return for instance to make and return a copy of a string which has been corrected so that "&" reads as "and".

*number
println(typeof(5)); and println(typeof(pileUpCounters())); which is a user defined function that returns a number, both returned 'number'
KA covers examples of how to use it.
... but println(typeof(draw())); returns 'number' which I did not expect at all. It's a function call, and I had no idea that it could return a number. The documentation does not say so.

*boolean
println(false); returns 'boolean' which is expected
println(typeof(1<3)); returns 'boolean' which makes sense to me as we've seen how 'return' works for booleans in Project Memory on KA.
KA covers of how to how to use it.

*undefined
println(typeof(sugar)); returns 'undefined' ,which is correct as I had not defined it
println(typeof(background(255)));  this is a function call for a function which does not return a value, I think. So it makes sense that it's undefined.
println(typeof(sugar())); makes OhNoes pop up saying 'sugar is not defined' which is correct, the function has not been defined. But it's problematic if I try to check the type. I had expected undefined since this is a function call which does not return anything

complex data types
*function
println(typeof(random)); returns a function, which is as expected
println(typeof(pileUpMoney)); returns a function because I have defined pileUpMoney as a function.

*object
println(typeof(canvas)); returns 'undefined' which surprised me at first because there is such as thing as the canvas object, but then I realized that it's probably because the canvas object is not a javascript object
var d = new Date(); println(typeof(d)); returns 'string' which I did not expect for something called a date object.which is a part of Javascript.
println(typeof(thingy)); returns 'object' when I've defined thingy as an object, which is expected

and a known problem child,
*null - can't create an example as the type of null is 'object' which I'll just have to accept as an anomaly or bug.

Larry Serflaten

unread,
Aug 24, 2018, 2:25:12 PM8/24/18
to Khan Academy Javascript Technical Q&A


 Inger Hohler wrote:
I am trying to explore 'return', closure etc. and wanted to try to find examples of returning different data types.
 
... but println(typeof(draw())); returns 'number' which I did not expect at all. It's a function call, and I had no idea that it could return a number. The documentation does not say so.


That may be KA's doing.  if you use;   println(draw);
You will see that it prints a function that returns 0.  I do not see that in the PJS code that I have (Vers. 1.4.8) In that code draw is simply an undefined variable.  Its the user who actually assigns the function.

 
println(typeof(sugar())); makes OhNoes pop up saying 'sugar is not defined' which is correct, the function has not been defined. But it's problematic if I try to check the type. I had expected undefined since this is a function call which does not return anything


It can not be resolved because 'typeof' can not access the object.

You could just define a function that does not return anything and test that:

var nope = function(){};
println(typeof nope());


And you can try to intercept the error before Oh Noes gets it:

try{
    println(typeof sugar());
}catch(e){
    println(e);
}


But it will tell you pretty much the same thing:  ReferenceError: sugar is not defined 


var d = new Date(); println(typeof(d)); returns 'string' which I did not expect for something called a date object.which is a part of Javascript.


Yes "d"  should be an object type, but KA still has issues with the Date object:


FYI:   typeof  is an operator,  not a function.  Check your W3schools examples, they do not use parentheses.


LFS

Bob Lyon

unread,
Aug 24, 2018, 5:25:48 PM8/24/18
to Khan Academy Javascript Technical Q&A
The typeof null is "object" which is neither an anomaly or bug.  null is the only object that when coerced to a boolean is false.  It shares that rare coercion status with zero (the only number), "" (the only string), and undefined.

Larry Serflaten

unread,
Aug 24, 2018, 7:08:21 PM8/24/18
to Khan Academy Javascript Technical Q&A


Bob Lyon wrote:
The typeof null is "object" which is neither an anomaly or bug.  null is the only object that when coerced to a boolean is false.  It shares that rare coercion status with zero (the only number), "" (the only string), and undefined.


That certainly makes more sense than the ECMA Specification:

"The value of the null literal null is the sole value of the Null type, namely null."


:-)
LFS 
Reply all
Reply to author
Forward
0 new messages