一、算术运算的注意事项
1、操作数不是数字时,Javascript会尝试将数据转换为数字,转换成功则进行算术运算,反之,则返回数字常量NaN:
- var a = "123";
- alert(++a); //输出124
- alert(a-2); //输出122
- alert("123ds" - 1); //输出NaN
var a = "123";
alert(++a); //输出124
alert(a-2); //输出122
alert("123ds" - 1); //输出NaN
</pre><span style="white-space: pre;"></span>2、加法运算符(+)</p><p></p><p></p><p><span style="font-size:18px;"><span style="white-space: pre;"></span>2.1 当两个或多个数字使用“+”运算符时,执行加法算术运算。</span></p><p><span style="font-size:18px;"><span style="white-space: pre;"></span>2.2 当单个数字和其他类型的数据使用“+”运算符时,首先会将两个表达式的值转为字符串,执行字符串运算<span style="white-space: pre;"></span>。如alert("10"+10); //输出1010</span></p><p><span style="font-size:18px;"><span style="white-space: pre;"></span>2.3 当多个数字和其他类型的数据使用<span style="font-size:18px;">“+”运算符时,首先会从左到右根据运算符两边的数据类型进行判断,是采用算术运算还是字符串运算。如alert("10"+10+5); //输出10105;而alert(5+10+"10"); //输出1510</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"></span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>二、全等运算符</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>1、数字和逻辑值按值比较,值相等,则返回true。</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>2、字符串具有相同字符数且对应位置的字符相同,则返回TRUE。</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>3、对于对象、函数和数组变量则是按引用比较,若引用同一个对象,则返回TRUE。</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"></span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>三、特殊值的比较</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space: pre;"></span>1、常量NaN:没有具体数值,解释引擎一般将其表示为一个随机对象。</span></span></p><p><span style="font-size:18px;"><span style="font-size:18px;"></span></span><div class="dp-highlighter bg_javascript"><div class="bar"><div class="tools"><strong>[javascript]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/u011043843/article/details/25883177#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/u011043843/article/details/25883177#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/u011043843/article/details/25883177#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/u011043843/article/details/25883177#">?</a><a target=_blank style="text-indent: 0px;" title="在CODE上查看代码片" href="https://code.csdn.net/snippets/346768" target="_blank"><img style="left: 2px; top: 1px; position: relative;" alt="在CODE上查看代码片" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" /></a><a target=_blank style="text-indent: 0px;" title="派生到我的代码片" href="https://code.csdn.net/snippets/346768/fork" target="_blank"><img style="left: 2px; top: 2px; position: relative;" alt="派生到我的代码片" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" /></a></div></div><ol class="dp-c"><li class="alt"><span><span>alert(NaN == NaN); </span><span class="comment">//false</span><span> </span></span></li><li><span>alert(NaN != NaN); <span class="comment">//true</span><span> </span></span></li><li class="alt"><span>alert(NaN >= NaN); <span class="comment">//false</span><span> </span></span></li><li><span>alert(NaN < NaN); <span class="comment">//false</span><span> </span></span></li></ol></div><pre style="display: none;" class="javascript" name="code" code_snippet_id="346768" snippet_file_name="blog_20140515_3_5872714">alert(NaN == NaN); //false
alert(NaN != NaN); //true
alert(NaN >= NaN); //false
alert(NaN < NaN); //false
2、null和undefined的比较:无规律,参考一下示例
- alert(null == undefined); //true
- alert(null != undefined); //false
- alert(null >= undefined); //false
- alert(null < undefined); //false
- alert(null === undefined); //false
alert(null == undefined); //true
alert(null != undefined); //false
alert(null >= undefined); //false
alert(null < undefined); //false
alert(null === undefined); //false
四:浅谈typeof、instanceof和delete运算符
1、typeof运算符用于测试表达式的类型,格式为 typeof(expression);
1.1用于特殊类型:
- typeof(NaN); //number类型
- typeof(Infinity); //number类型
- typeof(Undefined); //undefined类型
- typeof(null); //object类型
typeof(NaN); //number类型
typeof(Infinity); //number类型
typeof(Undefined); //undefined类型
typeof(null); //object类型
1.2用于Javascript的核心类,永远返回object:
- var blog = "myblog";
- var blog1 = new String("myblog");
- alert(typeof blog); //输出string
- alert(typeof blog1); //输出object
var blog = "myblog";
var blog1 = new String("myblog");
alert(typeof blog); //输出string
alert(typeof blog1); //输出object
2、instanceof运算符:测试表达式是否是指定类的实例
- var blog = "myblog";
- var blog1 = new String("myblog");
- alert(blog instanceof String); //false
- alert(blog1 instanceof String); //true
var blog = "myblog";
var blog1 = new String("myblog");
alert(blog instanceof String); //false
alert(blog1 instanceof String); //true
3、delete运算符:破坏对象属性,使其初始值为undefined,执行成功返回TRUE,但是delete不能删除对象的固定属性,如数组的length属性
- var user = new Object;
- user.name = "zhang";
- user.age = 20;
- var b = delete user.name;
- alert(b); //true
var user = new Object;
user.name = "zhang";
user.age = 20;
var b = delete user.name;
alert(b); //true