Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set an undefined variable as a function parameter? [duplicate]

I am building a function to match types of variables. It will be an alternative to typeof <var> === "something".

My function call looks like this : is("some text")["string"]. It returns true, or is([])["element"]. Now it returns false, But I have an issue with it.

For example, if I try to send an undefined variable like "undefVar" to a function I am expecting something like this: is(undefVar)["undefined"], but I get an error which says that "undefVar" is not defined.

Can I somehow make my function work using undefined variables? Is this possible at all?

p.s: I have to use this function a lot so it seems (for me) that it would be better to use something like this : is(var)[type] as opposed to typeof var === type.

like image 736
John Avatar asked Sep 23 '26 04:09

John


1 Answers

typeof is special on nonexistent variables. You cannot mimic it with a function.

like image 197
Kevin Reid Avatar answered Sep 24 '26 22:09

Kevin Reid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!