Check Undefined in JavaScript


Check Undefined in JavaScript : There are many ways to check undefined variable in javascript. Here in this tutorial we are going to explain the simplest methods to check for the undefined variables. In this tutorial we will explain this with example and demo.


How to Check Undefined in JavaScript | JS

You can use typeof to check if the variable is undefined.

Check Undefined in JavaScript : Example

<html>
<head>
<title>Javascript Demo</title>
<script type="text/javascript">
function checkUndefined() {
 if(typeof my_var === 'undefined' ){
alert("undefined variable");
}
}
</script>
</head>
<body>
<a href="javascript:void(0)" onclick="checkUndefined()">Check </a>
</body>
</html>

Try it »

The above example will check the given variable using the typeof method. If the variable is undefined it will alert the above message.

If you run the above example it will produce the following output as below –

How to Check Undefined in JavaScript | JS


Advertisements

Add Comment

📖 Read More