detect "null"
-
@ustk I never use
var == undefined
I always useisDefined(var)
I just noticed this guy commented on my video (I think I might re-record that one)
-
if I declare a variable like this
const t = null;
First I'll get
Console.print(t); API call with undefined parameter 0
Console.print(t == void); result: 1 Console.print(t == undefined); result: 1 Console.print(t == null); result: 1 Console.print(t === void); result: 0 Console.print(t === undefined); result: 0 Console.print(t === null); result: 1
but my variable show up as null, and if I use
Console.print(myvariable === null); result: 0
Are there different kind of "null"??
-
@ulrik Are you sure your variable is null?
-
@d-healey that's what it's saying if I Console.print it
my var is inside an array and if trace log it will say something like this:Interface: [ 3, null ]
-
@ulrik I think you need to give us an example we can test.
This works for me:
const a = [3, null]; Console.print(a[1] === null);
So you must be doing something different.
-
@ulrik Ok, I Console.print each member of the array and got this instead:
Interface: inf
so if trace logged it show up as "null" and if logged independently it show up as "inf"
-
-
@ulrik No idea. I'd recommend you try to always populate your variables yourself so you know what it contains.
-
Yes, inf is infinite:
Console.print(1.0 / 0.0)
But the question is: how did you get
null
in the first place? I honestly didn't know that existed, we've gotundefined
for a uninitialized variable,void
for a invalid array or object field andinf
andnan
for illegal number values.var x = 1 / 0.0; x = 0.0 * x; var a = [1]; var n; Console.print(typeof(a[9])); // => void Console.print(typeof(b)); // => undefined Console.print(typeof(1.0 / 0.0)); // => number Console.print(typeof(n)); // => undefined Console.print(x); // => nan
-
@Christoph-Hart ok thanks!
As I said here:
@ulrik Ok, I Console.print each member of the array and got this instead:
Interface: inf
so if trace logged it show up as "null" and if logged independently it show up as "inf"
It seems the
Console.print(trace(array))
is not showing correct information?
-
@ulrik I just skimmed through the JUCE codebase and the only time where
null
is used is when you convert a JSON object with a void field to a string (which would be the case if you do something likeConsole.print(trace(array)
command). I guess that's just for compatibility reasons with the JSON standard and it is still being typed internally asvoid
. I can't say why it is behaving like that with aninf
number though, as this is clearly a number type and should be treated as such (eg.isDefined(1.0/0.0)
returns true).However I got this output:
so apparently assigning
void
to a field is the same as assigningundefined
(which I thought were two different things), however theisDefined()
check works correctly on all of them.BTW, using
inf
as literal is not protected, so you can do stuff like:var inf = 90; Console.print(inf);
-
@Christoph-Hart Is it normal that
a[2]
set tonull
is throwingundefined
? It's weird to me because it is actually defined asnull
...EDIT: I think I have my answer...
console.log(null === undefined) // false (not the same type) console.log(null == undefined) // true (but the "same value")
-