Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >javascript当中null和undefined的==和===的比较

javascript当中null和undefined的==和===的比较

作者头像
马克java社区
修改于 2019-10-08 04:12:48
修改于 2019-10-08 04:12:48
6280
举报
文章被收录于专栏:java大数据java大数据

例 3.1.3(null和undefined的==和===的比较) <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> //马克-to-win:声明变量x /* if a value is not set, its typeof is undefined, its value is undefined, if a value= null, its typeof is object, its value is null,but when you use == to test, they are the same, but when to use === to test, they are not the same,== means as long as value is the same, ok, but === means type also must be equal. */ var x; var z1 = "d"; var y = null; // document.writeln("z1 is"+z1); /*if you cancel commenting out the following statement, it will give the blank page, which means it has error.*/ // document.writeln(zyx); document.writeln(x + " is x"); document.writeln(typeof(x) + " is typeof(x)"); document.writeln(y + " is y"); document.writeln(typeof(y) + " is typeof(y)"); var z = undefined; document.writeln(z + " is z"); document.writeln(typeof(z) + " is typeof(z)"); if (y == undefined) { document.writeln('null and undefined is interchangable'); } if (z1 != null) { document.writeln('z1 != null'); } if (y === undefined) { document.writeln('null and undefined is exactly the same'); } if (x == undefined) { document.writeln('声明变量后默认值为undefined'); } if (x === undefined) { document.writeln('声明变量后默认值为exactly the same as undefined'); } if (x == null) { document.writeln('声明变量后默认值为null'); }

更多请见:https://blog.csdn.net/qq_44594249/article/details/99689140

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
javascript当中静态方法和prototype用法
6)静态方法和prototype(难) 例 3.6.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*note that 马克-to-win: static variable's value has nothing to do with instance's variable's value.instance 名称 can not 直接access static member like in java. This is different from Java,比如下面例子中,Student.number=2,但是d1.number就为undefined.This is different from Java,但在实例方法中(比如d1.info)可以访问Student.number。这是和java中一样的。或者说function外或任何地方都可以访问Student.number。反过来,d1.age也可以在静态方法中访问,就像在function外一样,任何地方都能访问d1.age。String.prototype.abcd,这是给所有的实例加属性而不是静态属性。*/ function Student(number, agev) { this.age = agev; /*static variable's value can not be accessed by instance */ Student.number = number; /*lb is local variable, but not a member variable because it is not modified by this. from outside it can not be accessed. refer to noblockScope.html */ var lb = 0; } var d1 = new Student(1, 3); document.writeln("this的age属性为means window.age" + this.age + "<br>"); document.writeln("d1的age属性为" + d1.age + "<br>"); document.writeln("d1的number属性为" + d1.number + "<br>"); document.writeln("通过Student访问静态number属性为" + Student.number + "<br>"); document.writeln("d1的lb属性为" + d1.lb + "<br><hr>"); d1.qixy = "abc";/*以随意为实例加属性或方法*/ document.writeln("可以随意为实例加属性或方法see following,d1的qixy属性为" + d1.qixy + "<br><hr>"); document.writeln("是否有静态变量qixy" + Student.qixy + "<br><hr>"); d1.info = function()/*此方法仅为d1对象所用*/ { document.writeln("对象的qixy属性:" + this.qixy); document.writeln("对象的age属性:" + this.age); /*下列话是合法的, 因为不是this.number, 而是Student.number*/ document.writeln("static method is " + Student.number); }; Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/ { document.writeln("对象的qixy属性p:" + this.qixy); document.writeln("对象的age属性p:" + this.age);
马克java社区
2019/10/09
4800
javascript当中静态方法和prototype用法
javascript当中Object用法
9)Object 例 3.9.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*马克-to-win:When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ var oi = new Object(); oi.name = 'mark'; oi.height = 4; function xxx() { document.writeln("对象的name属性:" + this.name); document.writeln("<br>"); document.writeln("对象的height属性:" + this.height); } oi.list = xxx; oi.list(); document.writeln(oi["name"] + oi["height"]); </script>
马克java社区
2019/10/10
4150
javascript当中Object用法
javascript当中arguments用法
8)arguments 例 3.8.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /* when there are n functions with the same function name, only the last one is used. */ function test() { document.writeln("no argument constructor."); } function test(person) { document.writeln("马克-to-win2"); /*Function.arguments[] (Collection) The values passed to the function when it is called. 马克-to-win: inside function,we can directly use arguments. */ var n = arguments.length; document.writeln("n is " + n); for (var i = 0; i < n; i++) { document.writeln("马克-to-win3"); document.writeln(arguments[i]) } document.writeln(person); document.writeln(typeof(person) + "is typeof"); } /*when no param, undefined is passed in. no overloaded function concept.*/ test(); /*when there is no this function, program stops here.*/ change(); document.writeln("finally"); </script>
马克java社区
2019/10/10
4490
javascript当中arguments用法
javascript当中的构造函数的用法
5)构造函数的用法: 例 3.5.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> function Student(name, age) { /* 马克-to-win:later on we can use it in var doc = new ActiveXObject( "Microsoft.XMLDOM" ); doc.async="false"; doc.load(str); when a property has a this, means that this property is a member property. */ this.name = name; this.age = age; this.parti = function() { document.writeln("名字是:" + this.name + "<br>"); document.writeln("年纪是:" + this.age + "<br>"); }; } var p = new Student('jeri', 3); document.writeln("typeof p is " + typeof(p)); //typeof(p) is object p.parti(); p.age = 4; p.parti(); /*the following two methods can also access some properties.*/ document.writeln("" + p["age"]); document.writeln("" + p["a" + "ge"]); if (p instanceof Student) document.writeln("p是Student的实例<br>"); /*javascript 中的对象全部是Object 的子类 Because this object is the topmost parent object in the prototype inheritance hierarchy, all other object classes inherit its methods and properties. It's a close enough call that JavaScript 2.0 may well move it into the class-based object-oriented category at which time the prototype inheritance would be replaced with super-class/sub-class mechanisms and the arguments become null and void. */ /*When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ if (p instanceof Object) document.writeln("p是Object的实例"); </script>
马克java社区
2019/10/06
5390
javascript当中的构造函数的用法
javascript当中prototype用法
prototype 见上一节,马克-to-win:prototype作用就是给某个类增加一个实例方法。 例 3.6.2 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ Array.prototype.mymethod = function(number) { var result = -1; /*注意mymethod功能是找出某数在数组中出现的位置。作为Array的一个function,可以访问Array的属性this.length, 参见上一个prototype的例子, Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/ { document.writeln("对象的qixy属性p:" + this.qixy); document.writeln("对象的age属性p:" + this.age); /*下列话是合法的, 因为不是this.number, 而是Student.number*/ document.writeln("static method is " + Student.number); }; */ for (var i = 0; i < this.length; i ++) { if (this[i] == number) { result = i; break; } } return result; }
马克java社区
2019/10/10
3960
javascript当中prototype用法
javascript当中json用法
10)json 例 3.10.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> var student = { name : 'mark', age : 3 , classes : ['小' , '中' , "大"], /* 马克-to-win:class is an array of string, also parents is also an array of json object. */ parents :[ { name : 'father', age : 42, salary : 'low' } , { name : 'mother', age : 37, salary : 'high' } ] }; document.writeln(student.name); document.writeln("<hr>"); document.writeln(student.age); document.writeln("<hr>"); document.writeln(student.classes[1]); document.writeln("<hr>"); document.writeln(student.parents[1].name); </script>
马克java社区
2019/10/10
6080
javascript当中json用法
javascript当中类型转换,typeof的用法
1)类型转换,typeof的用法 例 3.1.1 <HTML> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <BODY> <SCRIPT LANGUAGE="JavaScript">     <!--     /*     Cast operator (Definition)  refer to 过去的网站www.favo.com     A way of converting data types.     Primitive values can be converted from one to another or rendered as objects by using object constructors to convert the values.     Boolean                                            Number                                             String                                             Number() (Function)  马克-to-win: actually Number() is the method of Global object.     A Number type convertor.     Property/method value type: Number primitive     JavaScript syntax: - Number()     - Number(aValue)     Argument list: aValue A value to be converted to a number.     When the Number() constructor is called as a function, it will perform a type conversion.     The following values are yielded as a result of calling Number() as a function:     Value                            Result     Number                            No conversion, the input value is returned unchanged.     Non numeric string                NaN     window.Number("23");在favo中查不出来, 但Idea中可以打点打出来。     */     var a = 9; /*下句话如果放在ie8中执行, 必须打开debug工具*/ //    console.log(typeof(a));     document.writeln(typeof(a));     var as = String(a);     //String是Global的方法     document.writeln("typeof(as) is " + typeof(as));     var x = window.Number("23");     document.writeln("typeof(x) is " + typeof(x));     var age2 = Number("56");     document.writeln(typeof(age2) + "is typeof(age2)");     var age3 = new Number(56);
马克java社区
2019/10/04
7900
javascript当中类型转换,typeof的用法
javascript当中函数指针用法
12)函数指针 例 3.12.1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript"> function sortNumber(a, b) { document.writeln(a + b); } function sortq(sortNumberqqq) { document.writeln("马克-to-win"); sortNumberqqq(1, 2); return 6; } /* note that it will report error if we write the following statement as document.write("test "+sortq(sortNumberqixy)); note that here sortNumber is a function pointer. 下面这句话是先执行sortq(sortNumber),等返回值以后,再执行document.write("test "*/ document.write("test " + sortq(sortNumber));
马克java社区
2019/10/11
7030
javascript当中函数指针用法
javascript当中局部变量和全局变量
2)局部变量和全局变量 浏览器里面 window 就是 global,通常可以省。 nodejs 里没有 window,但是有个叫 global 的。 例 3.2.1 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /* 有var无var, 在function外是一样的,都是全局的,在function里面时,var是局部的,而无var时是代表全局的*/ var testVar = "全量"; document.writeln("window.testVar is" + window.testVar+testVar); var testqVar = "全量q"; /*如不屏蔽下句话,程序直接停在这了,因为出错了,不认识testGlobal,得把下一句和下下句换一下位置,就ok了 */ // document.writeln("testGlobal is" + testGlobal); testGlobal = "全量global"; document.writeln("abc is" + abc); var abc; testGlobalInVar = "全量globalInVar"; function testSco() { var lll = "qqq"; var testVar = "局量"; //此testVar非外面的testVar testqVar = "全量qchange"; //此testqVar就是外面的testqVar testGlobal = "全量globalchange"; var testGlobalInVar = "局量global";//此testGlobalInVar非外面的testGlobalInVar /*local variable is stronger than global variable.so "testVar" in the following statement means local variable.*/ document.writeln(testVar); document.writeln(testqVar); document.writeln("testGlobalInVar is " + testGlobalInVar); } testSco(); document.writeln("second test is " + testVar); document.writeln("second testqVar is " + testqVar); document.writeln("testGlobal is " + testGlobal); document.writeln("testGlobalInVar is " + testGlobalInVar); </script>
马克java社区
2019/10/05
5560
javascript当中局部变量和全局变量
javascript当中Function用法
4)Function用法 例 3.4.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*马克-to-win:When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ /*Function的好处是, 函数体可以是运行时的一个传入的动态字符串,for the Function class, the last parameter is function body, while the parameters before the last one are input arguments., 'x' can be changed to "x" */ var a='return x + y;'; var f = new Function('x', 'y',a );// 等价于var f = function(x, y){return x + y;} document.write(f(1, 1)); </script>
马克java社区
2019/10/06
3400
javascript当中Function用法
javascript当中call()和apply()用法
13)call()和apply() call是在特定的作用域中调用函数。 例 3.13.1 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title> </title> <script type="text/javascript"> function A(name) { this.name = name; } A.prototype.info = function() { /*如果下局解开屏蔽,最后结果则打印出就为A,结论就是在call时,先在A里找this.name,如果找不着,则用b环境里找,现在A的构造函数从来没有执行过,所以最后用的是B环境的name,见下一个例子*/ //this.name="A"; return "A的名字是 "+this.name; } function B(agev) { this.age=agev; this.name="I am B" } var b = new B(2); var tmp =A.prototype.info; document.writeln(tmp.call(b)); var zhanwei="zhanwei"; </script> </head> <body>
马克java社区
2019/10/11
4970
javascript当中call()和apply()用法
给出一个javascript的Helloworld例子
<!-- 如果你用notepad建立一个txt之后你再改为html,一定在存时,要存成utf-8或unicode格式,或者你也可以用 myeclipse html designer,这样你看的文本是有颜色的,如果觉得字体小,可以在myeclipse html designer下面的窗口里右击鼠标,/preferences/general/editor/text editor.注意在texteditor窗口里面的右边最下面,有一行不起眼你的小字,color and font,你可以设置。-->
马克java社区
2019/10/03
4230
给出一个javascript的Helloworld例子
javascript当中for in Array用法
11)for in Array 例 3.11.1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <script type="text/javascript"> var a=['hello','teacher','马克-to-win']; for(var iii in a){ this.document.write('inidex'+iii+'的值是'+a[iii]+"<br>"); } </script> </head> <body> </body> </html> 更多请见:https://blog.csdn.net/qq_44594249/article/details/100123160
马克java社区
2019/10/10
3870
javascript当中for in Array用法
HTML5+CSS3+JavaScript从入门到精通-12
HTML5+CSS3+JavaScript从入门到精通 作者:王征,李晓波 第十二章 Javascript编程的初步知 案例 12-01 JavaScript的数值常量 <!DOCTYPE html> <!--web12-01--> <!-- JavaScript是弱类型: 即使用前无需声明,使用或赋值时确定其数据类型。 Infinity: 正无穷大,也可以加负号 NaN:Not a number,运算(比如0除以0)产生未定义结果时的值。它与任何值都不相等,包括自己在内。
qiqi_fu
2021/12/03
9180
HTML5+CSS3+JavaScript从入门到精通-12
原生JavaScript第二天
1.typeof操作符 typeof操作符用来检测变量的数据类型 2.数据类型 2.1Undefined类型,只有一个值undefined(在使用var声明变量没有初始化的时候,这个变量数据类型的值就是undefined) 注意:没有初始化和没有声明的变量是不一样(虽然typeof返回的值都是undefined,但是没有声明的会报错的) 2.2Null类型,只有一个值null,typeof操作符检测null返回object(这样就可把将来要保存对象的变量初始化为null) 注意:underfined和nul
苦咖啡
2018/05/08
5050
javascript当中嵌套函数
3)嵌套函数 例 3.3.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> function outerFun(){ var i = 0; function innerFun(){ document.write(i); } innerFun(); } outerFun(); /*here you can not call innerFun(), because it is inside another function outerFun, so it will cause error.*/ innerFun(); document.write("马克-to-win"); </script>
马克java社区
2019/10/05
5260
javascript当中嵌套函数
给出一个javascript的Helloworld例子
<!--  马克-to-win:如果你用notepad建立一个txt之后你再改为html,一定在存时,要存成utf-8或unicode格式,或者你也可以用 myeclipse html designer,这样你看的文本是有颜色的,如果觉得字体小,可以在myeclipse html designer下面的窗口里右击鼠标,/preferences/general/editor/text editor.注意在texteditor窗口里面的右边最下面,有一行不起眼你的小字,color and font,你可以设置。-->
马克java社区
2020/11/27
5330
给出一个javascript的Helloworld例子
JavaScript中的数据类型
在ECMAScript中,变量是松散类型的。所谓松散类型就是指变量可以用来保存任何类型的数据。  // 下面的操作是完全合法的 var message = "helloWorld"; message = 100; 但是在实际开发中,我并不推荐大家这样使用变量。这种操作方法是会让代码变得很不安全。为了规避这样的问题,我在变量命名的时候对变量类型做了标明。 var strMessage = "helloWorld"; // String类型 var nMessage = 100; // Number类型 //
就只是小茗
2018/03/07
2.3K0
JavaScript中的数据类型
JavaScript学习总结(一)
前端三剑客,html、css、js。 这三种语言基本是前端开发必备的东西,那么你知道这三种语言分别负责的功能是什么吗?
roobtyan
2019/02/21
1.1K0
javascript当中concat,join,slice用法
例 1.3(concat,join,slice) <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <Script> /* 马克-to-win:qixy: Array is a dynamic array. - myArray = new Array(aLength) - myArray = new Array(anItem1, anItem2, anItem3, ...) */ var arr = new Array(1);//arr.length is 1,但是里面的东西是undefined, 所以这样写[undefined] /*虽然arr是个object, 但是里面的东西是undefined*/ document.write("typeof arr is "+typeof(arr)); var a = new Array(2, 6, 5, "a"); document.write("arr.length is "+arr.length+"a.length is "+a.length); var b = new Array(12, 14); arr[0] = "java"; arr[1] = "intel"; arr[2] = "microsoft"; /* Property/method value type: Array object JavaScript syntax: - myArray.concat(someValues, ...) The result of this method is a new array consisting of the original array, plus the concatenation. The values that are passed to the method are added to the end of the array. If arrays are passed, they are flattened and their individual elements added. The method returns an array consisting of the original Array plus the concatenated values. If Array1 contains "AAA", "BBB", "CCC" and Array2 contains "000", "111", "222", then the method call Array1.concat(Array2) will return an array with all the elements in a single collection. The original arrays will be untouched. */ document.write("arr.toString() is " + arr.toString() + "<br>"); //与无参join()方法等同 var arrconcat=arr.concat(a, b); document.write("arr.concat(a,b) is " + arrconcat + "<br>"); /*Array.join() (Method) Concatenate array elements to make a string. Property/method value type: String primitive JavaScript syntax: - myArray.join(aSeparator) Argument list: aSeparator A string to place between array elements as the array is concatenated to form a string. //无参join()方法默认是用逗号连接 */ document.write("arr.join() is " + arr.join
马克java社区
2019/10/13
5410
javascript当中concat,join,slice用法
相关推荐
javascript当中静态方法和prototype用法
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档