I have code that is wrapped in try/catch block.
I use typeof to find out if a variable is defined:
if (typeof (var) == 'string') {
//the string is defined
}
However, using this in a try/catch block, jumps to the catch part instead of doing what it is suppoed to do (do something with the string if its defined).
How can I check if a variable is defined without activating an exception?
‘var’ is not a valid variable name – it’s a keyword.
Apart from that, what you have should be correct.