I am Hack Sparrow
Captain of the Internets.

Check if a variable is defined in JavaScript

The following example shows how to check if a variable is defined or not in JavaScript.Using typeof Operator

if (typeof username == 'undefined') console.log('Variable is not defined');else console.log('Variable is defined');
Note that undefined is quoted in the above code snippet. It won't work if it's not quoted. Try it.

Make a Comment