detect "null"
-
How can I detect "null"?
I have a variable holding a number, and in some cases it will return as a "null" and I want to detect when that happens, so how can Iif (variable == bla...) do something else...I tried "undefined", "isDefined(), and it seems that "null" is defined, so that will not work.
I tried
Console.print(typeof var);which results in "number"
-
@ulrik this will not work either
if (r === null) -
@ulrik What about preventing it being null?
-
@ulrik Ok I solved it in this case, this worked
if (variable == "inf") break; -
@d-healey I often use
undefinedornullas intentional state or value for variables (especially for object properties) so I can discriminate if a value is ready to read or not. So when I want to reset it I set it back toundefinedornull
Let’s say it my-1EDIT: I realise that I should more often use
nullrather thanundefined -
@ulrik said in detect "null":
@ulrik this will not work either
if (r === null)It works for me. Try this.
const t = null; Console.print(t == void); Console.print(t == undefined); Console.print(t == null); Console.print(t === void); Console.print(t === undefined); Console.print(t === null); -
@ulrik you can use shorthand
if (!var) { } -
@parabuh That won't tell you is the variable is null
-
@d-healey Hmmm... I just discovered that a value of
0 == undefined
Back to school for me! -
@parabuh that will not work
-
@ustk I never use
var == undefinedI always useisDefined(var)I just noticed this guy commented on my video (I think I might re-record that one)

-
@d-healey
if I declare a variable like this
const t = null;First I'll get
Console.print(t); API call with undefined parameter 0Console.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: 1but my variable show up as null, and if I use
Console.print(myvariable === null); result: 0Are 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: infso if trace logged it show up as "null" and if logged independently it show up as "inf"
-
@ulrik @d-healey so what does "inf" stand for, infinite?
-
@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
nullin the first place? I honestly didn't know that existed, we've gotundefinedfor a uninitialized variable,voidfor a invalid array or object field andinfandnanfor 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: infso 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?