Check Whether a Variable or Property Is null or undefined in JavaScript
Description
The following code shows how to check Whether a Variable or Property Is null or undefined.
Example
<!-- w w w . j a va 2 s.c om-->
<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var myData = {
name : "JavaScript",
city : null
};
if (!myData.name) {
document.writeln("name IS null or undefined");
} else {
document.writeln("name is NOT null or undefined");
}
if (!myData.city) {
document.writeln("city IS null or undefined");
} else {
document.writeln("city is NOT null or undefined");
}
</script>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Null
Assign undefined value to a variable in Jav...
Assign variable to NULL value in JavaScript
Check if a variable has been assigned a val...
Create a null value with null in JavaScript
Create undefined value by defining a variab...
Differentiate between null and undefined, y...
Find out the difference between undefined v...
Treat undefined the same as null, you can u...
Use typeof operator on null in JavaScript
Assign undefined value to a variable in Jav...
Assign variable to NULL value in JavaScript
Check Whether a Variable or Property Is nul...
Check if a value is undefined in JavaScriptCheck if a variable has been assigned a val...
Create a null value with null in JavaScript
Create undefined value by defining a variab...
Differentiate between null and undefined, y...
Find out the difference between undefined v...
Treat undefined the same as null, you can u...
Use typeof operator on null in JavaScript