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.