DEV Community

Discussion on: TypeScript Optional Parameters

Collapse
 
ecyrbe profile image
ecyrbe • Edited

You don't need to do a runtime typeof JavaScript introspection. Indeed, here c is declared. Use typeof only to check if a variable is declared.

if ( c === undefined )
Enter fullscreen mode Exit fullscreen mode

Just works in your example.

You can use typeof to check for global variables. For example :

window === undefined
Enter fullscreen mode Exit fullscreen mode

will throw an exception on nodejs when it will return false on your browser.
In this case, yes you should use typeof:

typeof window === "undefined"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
smpnjn profile image
Johnny Simpson

You're right, in fact typeof c == undefined won't work at all since typeof returns a string. I've updated.