In TypeScript, you can check if a value is null or undefined using the ===
operator to compare against null
or undefined
. You can also use the typeof
operator to determine the type of the value.
Here is an example of how to check if a value is null or undefined:
index.ts216 chars12 lines
In this example, x
can be a number, null, or undefined. We first check if x
is null, and if so, we print a message. If x
is not null, we check if it is undefined, and if so, we print a different message. If x
is neither null nor undefined, we know that it is a number.
Alternatively, we can use the typeof
operator to check the type of x
, like this:
index.ts235 chars12 lines
In this example, we first check if the type of x
is "undefined", and if so, we print a message. If x
is not undefined, we check if it is null, and if so, we print a different message. If x
is neither undefined nor null, we know that it is a number.
gistlibby LogSnag