There are two special values in JavaScript: undefined and null, and you need to be careful when comparing them. What you get when reading unassigned variables or trying to read properties that the object does not have is the undefined value.
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Learn4UndefinedAndNull</title></head><body><script> var myData = { name:"Luka", weather:"sunny" }; document.writeln("Prop: "+myData.doesnexits);</script></body></html>Output result:
Prop: undefined
Javascript also defines a special value null, which is slightly different from undefined. The latter is a value obtained when the value is not defined, while the former is used to indicate that a value has been assigned but is not a valid object, string, number, or boolean value (that is, a value that is defined is a valueless [no value]).
The following code uses undefined and null to show their different effects:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Learn4UndefinedAndNull</title></head><body><script> var myData = { name:"Luka" }; //Read weather attribute document.writeln("Var: "+myData.weather+"<br />"); //Determine whether the object has weather attribute document.writeln("Prop: "+("weather" in myData)+"<br /><br />"); myData.weather = "sunny"; document.writeln("Var: "+myData.weather+"<br />"); document.writeln("Prop: "+("weather" in myData)+"<br /><br />"); myData.weather = null; document.writeln("Var: "+myData.weather+"<br />"); document.writeln("Prop: "+("weather" in myData)+"<br /><br />");</script></body></html>Output result:
Var: undefinedProp: falseVar: sunnyProp: trueVar: nullProp: true
1. Check whether the variable or property is undefined or null
If you want to check whether a property is null or undefined (regardless of which one), just use the if statement and the logical non-operator (!).
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Learn4UndefinedAndNull</title></head><body><script> var myData = { name:"Luka", city:null }; if(!myData.name){ document.writeln("name is null or undefined <br /><br />"); }else { document.writeln("name is not null or undefined <br /><br />") } if(!myData.city){ document.writeln("city is null or undefined <br /><br />"); }else { document.writeln("city is not null or undefined <br /><br />") } if(!myData.weather){ document.writeln("weather is null or undefined <br /><br />"); }else { document.writeln("weather is not null or undefined <br /><br />") }</script></body></html>Output result:
name is not null or undefinedcity is null or undefinedweather is null or undefined
2. Distinguish between null and undefined
When comparing two values, the method used should be determined according to your needs. If you want to treat undefined and null values equally, you should use the equality operator (==) to let Javascript perform type conversion. At this time, a variable with an undefined value will be considered equal to a variable with a value of null. If you want to distinguish between null and undefined, you should use the equivalent operator (===).
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Learn4UndefinedAndNull</title></head><body><script> var firstVal = null; var secondVal; var equality = firstVal == secondVal; var identity = firstVal === secondVal; document.writeln("Equality: "+equality+" <br />"); document.writeln("Identity: "+identity+" <br />"); document.writeln("Identity: "+identity+" <br />"); />");</script></body></html>Output result:
Equality: trueIdentity: false
The above Javascript basics_simple comparison of undefined and null values is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.