JavaScript 中有兩個特殊值:undefined and null,javascript
<!DOCTYPE HTML> <html> <head> <title>Example</title> </head> <body> <script type="text/javascript"> var myData = { name: "admin", weather: "sunny" }; document.writeln("prop:" + myData.doesntexist); </script> </body> </html>
輸出:html
prop: undefined
例子:html5
<!DOCTYPE HTML> <html> <head> <title>Example</title> </head> <body> <script type="text/javascript"> var myData = { name: "admin" }; document.writeln("var:" + myData.weather); document.writeln("prop:" + ("weather" in myData)); document.writeln("</br>"); myData.weather = "sunny"; document.writeln("var:" + myData.weather); document.writeln("prop:" + ("weather" in myData)); document.writeln("</br>"); myData.weather = null; document.writeln("var:" + myData.weather); document.writeln("prop:" + ("weather" in myData)); </script> </body> </html>
輸出:java
var:undefined prop:false var:sunny prop:true var:null prop:true
例子:ide
<!DOCTYPE HTML> <html> <head> <title>Example</title> </head> <body> <script type="text/javascript"> var firstVal = null; var secondVal ; var equality = firstVal == secondVal; var identity = firstVal === secondVal; document.writeln("equality:" + equality); document.writeln("</br>"); document.writeln("identity:" + identity); </script> </body> </html>
輸出:ui
equality:true identity:false