Language Basics(Chapter 3 of Professional JavaScript® for Web Developers 2nd Edition)

本文深入探讨了JavaScript中变量类型的判断、数值与布尔值转换、字符串与数字转换、空值与未定义值的特性,以及不同类型的比较操作符在实际应用中的行为。通过具体示例代码,展示了等号(==)与恒等号(===)的区别,帮助开发者理解在进行变量比较时如何避免常见的陷阱。

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

  The end result was for ECMAScript to provide two sets of operators: equal and not equal to perform conversion before comparison, and identically equal and not identically equal to perform comparison without conversion.
ExpandedBlockStart.gifequality
 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 
 5 window.onload = function()
 6 {
 7     console.log("------------Equality Operator-------------");
 8     console.log("null == undefined: " + (null == undefined));
 9     console.log("'NaN' == NaN: " + ("NaN" == NaN));
10     console.log("5 == NaN: " + (5 == NaN));
11     console.log("NaN == NaN: " + (NaN == NaN));
12     console.log("NaN != NaN: " + (NaN != NaN));
13     console.log("false == 0: " + (false == 0));
14     console.log("true == 1: " + (true == 1));
15     console.log("true == 2: " + (true == 2));
16     console.log("undefined == 0: " + (undefined == 0));
17     console.log("null == 0: " + (null == 0));
18     console.log("'5' == 5: " + ('5' == 5));
19     
20     console.log("-----Identically Equality Operator------");
21     console.log("null === undefined: " + (null === undefined));
22     console.log("'NaN' === NaN: " + ("NaN" === NaN));
23     console.log("5 === NaN: " + (5 === NaN));
24     console.log("NaN === NaN: " + (NaN === NaN));
25     console.log("NaN !== NaN: " + (NaN !== NaN));
26     console.log("false === 0: " + (false === 0));
27     console.log("true === 1: " + (true === 1));
28     console.log("true === 2: " + (true === 2));
29     console.log("undefined === 0: " + (undefined === 0));
30     console.log("null === 0: " + (null === 0));
31     console.log("'5' === 5: " + ('5' === 5));
32 };
33 </script>
34 </head>
35 <body>
36 </body>
37 </html>

 

 

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 
 5 function howManyArgs()
 6 {
 7     console.log("Count of arguments is " + arguments.length);
 8 }
 9 
10 function addSomeNumber(num)
11 {
12     return num + 100;
13 }
14 
15 function addSomeNumber(num)
16 {
17     return num + 200;
18 }
19 
20 window.onload = function()
21 {
22     howManyArgs("string"45);
23     howManyArgs();
24     
25     console.log("addSomeNumber(100) is " + addSomeNumber(100));
26 };
27 
28 function setName(obj)
29 {
30     obj.name = "Summer";
31     obj = new Object();
32     obj.name = "autumn";
33 }
34 var person = new Object();
35 setName(person);
36 console.log(person.name);
37 
38 </script>
39 </head>
40 <body>
41 </body>
42 </html>

 

 

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 
 5 window.onload = function()
 6 {
 7     console.log("---------------------typeof-----------------------");
 8     var a;
 9     console.log("typeof undefined is '" + typeof(a) + "'");
10     
11     var b = null;
12     console.log("typeof null is '" + typeof(b) + "'");
13     
14     var c = true;
15     console.log("typeof true is '" + typeof(c) + "'");
16     
17     var d = 90;
18     console.log("typeof 90 is '" + typeof(d) + "'");
19     
20     var e = "hello";
21     console.log("typeof hello is '" + typeof(e) + "'");
22     
23     var o = new Object();
24     console.log("typeof Object is '" + typeof(o) + "'");
25     
26     console.log("----------Boolean Conversion-------------");
27     
28     var emptyString = "";
29     if(emptyString)
30     {
31         console.log("Empty string is converted to true");
32     }
33     else
34     {
35         console.log("Empty string is converted to false");
36     }
37     
38     if(b)
39     {
40         console.log("null is converted to true");
41     }
42     else
43     {
44         console.log("null is converted to false");
45     }
46     
47     if(a)
48     {
49         console.log("undefined is converted to true");
50     }
51     else
52     {
53         console.log("undefined is converted to false");
54     }
55     
56     var zero = 0;
57     if(zero)
58     {
59         console.log("zero is converted to true");
60     }
61     else
62     {
63         console.log("zero is converted to false");
64     }
65     
66     console.log("-----------------Number Conversion-----------------");
67     
68     console.log(Number("Hello Ray"));
69     console.log(Number(""));
70     console.log(Number("0010"));
71     console.log(Number(null));
72     console.log(Number(true));
73     
74     console.log("---------------parseInt---------------");
75     console.log(parseInt("123abc"));
76     console.log(parseInt("123abc"16));
77     console.log(parseInt(""));
78     console.log(parseInt("0xA"));
79     console.log(parseInt(22.5));
80     console.log(parseInt("070"));
81     
82     console.log("---------------parseFloat---------------");
83     console.log(parseFloat("22.34.5"));
84     console.log(parseFloat("3.12e4"));
85 }
86 
87 </script>
88 </head>
89 <body>
90 </body>
91 </html> 

转载于:https://www.cnblogs.com/zhtf2014/archive/2011/01/10/1932272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值