javascript---null,undefined,var

本文探讨了JavaScript中null和undefined的区别与联系,包括它们的含义、如何转换为其他类型及如何正确区分这两种值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

null

null is a keyword in javascript,which is a special value that indicates no value,a value that represents no object, if a variable holds the value null,it means that the variable contains nothing.

   In the last topic,I said that when null value converts to boolean ,it is false,to numeric ,it's 0,and when used in a string context,it converts to "null"(it is a string context);

    someting examples of a null value,we use document.getElementById("testID"),if the form does not contain a control which id is named "testID",the document.getElementById("testID") will return null.

 

ContractedBlock.gifExpandedBlockStart.gif代码
window.onload = function() {
var nullVar = document.getElementById("ttt");
alert(Number(nullVar));
//as number,alert 0
alert(Boolean(nullVar)); //as boolean,alert false
alert(nullVar);//as string,//alert null
}

in the above code ,document.getElementById("ttt") return null.

   you can't use any properties of null value,in the above value,if you use nullVar.value or nullValue.toString(),there will be a error occured.so it does in C#(in C# ,if we uses properties of a null object,a error occurs too).in other word,null values noting.

 

 

undefined

   undefined is returned when you use either a variable that has been declared but never had a value assigned to it or an object property that does not exist.

    some data conversions are in the eg. code as follows:

 

ContractedBlock.gifExpandedBlockStart.gif代码
window.onload = function() {
var undfVar
alert(undfVar);
//as string,alert undefined
alert(Boolean(undfVar)); //as boolean ,alert false
alert(Number(undfVar));//as number ,alert NaN
}

 

in this code,why undfVar is undefined ,because it was declared but never had a value assigned to it,so it is undefined.

  in other way,if we use the object property that does not exist, it returns undefined too,just eg.

(there is a input control in the body of current form)

window.onload = function() {
var proUndf = document.getElementById("existInput");
alert(proUndf.Alex);
}

proUndf represents a exist input control,as we know,input does not have a property that is named Alex;so proUndf.Alex returns undefined;

comparison  between null and undefined

    Although   null and undefined value are distinct,the ==operator considers them equal to each other;if we truely distinguish between a null value and an undefined value, we can use === operator or typeof function,eg.

 

ContractedBlock.gifExpandedBlockStart.gif代码
window.onload = function() {
var proUndf = document.getElementById("existInput");
alert(proUndf.Alex
== null); //alert true
alert(proUndf.Alex === null); //alert false
alert(typeof(proUndf.Alex) === typeof(null)); //alert false

}

 

 

undefined is not a keyword in javascript while null is.

 

var

    var is used to define a vailable,any types,because javascript is untyped (on the other hand ,C# is typed),I do not want to discuss the details of var.just want some specials.

    in javascript we can also declare a varialbe without var.when you assign  a value to a variable that not be declared  with var,javascript will implicitly declare that variable and assign the value to it,but,the implicitly declared varialbe will be treated as a global variable,even if it is just used in a function,eg;

 

ContractedBlock.gifExpandedBlockStart.gif代码
window.onload = function() {

myName
= "Alex";
alert(myName);
//alert Alex
test();
alert(myName);
//alert AlexHe,it has been changed by the function of test(),
}

function test()
{
alert(myName);
//alert Alex
myName = "AlexHe";
}

 

so we must be in habit of using var to declare a varialble whether which is a global variable or a local variable.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值