[JavaScript] null and undefined

null

null is usually considered a special value of object type, a value that represents no object.

When a variable holds the value null, you know that it does not containavalidobject, array, number, string or boolean vlue.

Boolean context-->false; numeric context-->0; string context-->"null"

undefined

undefined is returned

1.     Use a variable that has been declared but never had a value assigned to it

2.     An object property that does not exist

Difference between null and undefined

Note that this special undefined value is not the same as null

undefined==null  //true

type of null //object

type of undefined //undefined

type of xyz //undefined (define, but not assign)

alert(abcd) //  (even not define), cause error,

type of abcd //undefined (even not define), only typeof won’t cause declaring a globle variable

abcd=’my new value’ //  (even not define), assigning a value to it causes declaring a global variable instead of an error

type of objValue // undefined (var objValue)

type of objValue // object (var objValue = null;)

Although you can get away with not using the var statement when you write code in the global scope, you must always use var to declare local variables.

scope = "global";            // Declare a global variable,even without var

function checkscope( ) {

   scope = "local";         // Oops! We just changed the global variable

   document.write(scope);   // Uses the global variable

   myscope = "local";       // This implicitly declares an new global variable

   document.write(myscope);// Uses the new global variable

}

var j = 100;

function test(o) {

   if (typeof o == "object") {

       var j = 0;                 // j is defined everywhere, not just block

   }

   document.write(j);             // j is defined, but may not be initialized

}

test(null): 0, because null is object type, and the declared j is assignted 0

test(undefined): undefined, because undefined is undefined type not object type, and wedeclared j in the block, but we do not assign value to it

Assigning a value to an undeclared variable does not cause an error; instead, it implicitly declares the variable in the global scope.

There are two different kinds of undefined variables

一.  Never been declared

Assume u is never declared

Reading the value of it causes a runtime error, because it simply does not exist

alert(u); // Reading the value of u which never been declared causes an error

Assigning a value to it causes declaring a global variable instead of an error

u = 3; // Assigning a value to an undeclared variable creates the a global variable.

二.  Declared but never been assigned

Reading the value of it, you obtain its default value, undefined

You can demo it by the following code:

<html>
	<head>
		<script type="text/javascript">
						function demoUndefinedAndNull(){
							alert("1. First section");
							alert(undefined==null); //true
							alert(typeof null); //object
							alert(typeof undefined); //undefined
							alert(null); //null
							alert(undefined); //undefined
							
							alert("2. Second section");
							assignCauseDeclareGlobalVar="my new value"; //(even not define),
							// assigning a 
							// value to it causes DECLARING 
							// a global variable instead of an error
							alert(assignCauseDeclareGlobalVar); //my new value
							
							alert("3. Third section");
							// alert(myNotDefineVariable);//(even not define), cause error
							alert(typeof abcd); //undefined (even not define), 
							// only typeof WON'T cause declaring a globle variable
							// alert(abcd); //(even not define), cause error
							
							alert("4. Forth section");
							var objValue1;
							alert(typeof objValue1);//undefined (define, but not assign)
							alert(objValue1);//undefined
							
							var objValue2 = null;
							alert(typeof objValue2);//object (an assigned local variable)
							alert(objValue2);//null
							
							var objValue3 = "This is objValue3 value";
							alert(typeof objValue3);//string (an assigned local variable)
							alert(objValue3);//This is objValue3 value
							
							var objValue4 = 123.090;
							alert(typeof objValue4);//number (an assigned local variable)
							alert(objValue4);//123.09
						}
		</script>
	</head>

	<body>
		<form>
			<input type="button" name="demojavascript" value="Demo undefined and null" 
							οnclick="demoUndefinedAndNull();"/>
		</form>
	</body>
	
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值