ในการรับผลลัพธ์ต่อไปนี้ คุณสามารถค้นหาว่าตัวแปรเป็นค่าว่างหรือไม่ได้กำหนดไว้ หากผลลัพธ์เป็น "เท็จ" แสดงว่าตัวแปรนั้นเป็นค่าว่างและไม่ได้กำหนดไว้
ที่นี่ ผลลัพธ์ของตัวแปรเป็น "จริง" -
<html>
<body>
<script>
var age = 10;
if(age){
document.write("True");
} else{
document.write("False");
}
</script>
</body>
</html> ใช้ “typeof” เพื่อตรวจสอบว่าตัวแปรมีอยู่หรือไม่ -
<html>
<body>
<script>
var age = 10;
if( typeof age !== 'undefined' ) {
document.write("True");
} else{
document.write("False");
}
</script>
</body>
</html>