运算符——JavaScript
Posted zealifree005
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运算符——JavaScript相关的知识,希望对你有一定的参考价值。
一.什么是表达式
表达式是ECMAScript中的一个“短语”,解析器会通过计算把它转换成一个值。最简单的表达式是字面量或者变量名。例如:
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.96</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//数值字面量</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Lee'</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//字符串字面量</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//布尔值字面量 </span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//空值字面量</span> /Java/ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//正则表达式字面量</span> x:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, y:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//对象字面量、对象表达式</span> [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>] <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//数组字面量、数组表达式</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(n)</span> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> x+y; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//函数字面量、函数表达式</span> box <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//变量</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
- 通过合并简单的表达式来创建复杂的表达式。比如:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">box + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.96</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//加法运算的表达式</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typeof</span>(box) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查看数据类型的表达式</span> box > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//逻辑运算表达式</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
- 单一的字面量和组合字面量的运算符都可称为表达式
二.一元运算符
只能操作一个值的运算符叫做一元运算符
1.递增++和递减–
2.前置和后置的区别
没有赋值操作,前置和后置是一样的
有赋值操作时
前置的运算符会先累加(累减)再赋值
后置运算符则先赋值再累加或累减
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> age = ++box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//age值为101</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> height = box++; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//height值为100</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
-
3.其他类型应用一元运算符的规则
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'89'</span>; box++; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//90,数值字符串自动转换成数值</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ab'</span>; box++; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,字符串包含非数值转成NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>; box++; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1,false转成数值是0,累加就是1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.3</span>; box++; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.3,直接加1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1,不设置toString或valueOf即为NaN</span> toString : <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; ; box++; </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
-
4.加和减运算符
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>; +box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//100,对于数值,不会产生任何影响</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'89'</span>; +box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//89,数值字符串转换成数值</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ab'</span>; +box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,字符串包含非数值转成NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>; +box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//0,布尔值转换成相应数值</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.3</span>; +box; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.3,没有变化</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1,不设置toString或valueOf即为NaN</span> toString : <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; ; +box; </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
-
三.算术运算符
- 1.加法
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//等于3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> + <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NaN</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,只要有一个NaN就为NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> + <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> + -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> + -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,正无穷和负无穷相加等NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'100'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//100100,字符串连接符,有字符串就不是加法</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'您的年龄是:'</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//您的年龄是:1020,被转换成字符串</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span> + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'是您的年龄'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//30是您的年龄,没有被转成字符串</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'您的年龄是:'</span> + (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//您的年龄是:30,没有被转成字符串</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> + ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//10[object Object]类型是string</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> + 对象 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//10[object Object],如果有toString()或valueOf(),则返回10+返回数的值</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
- 2.减法
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//等于30</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//等于-170</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-30,一般写成-100 - (-70)比较清晰</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> - <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NaN</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,只要有一个NaN就为NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> - <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> - -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> - -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> - <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//99,true转成数值为1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//100,''转成了0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'70'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//30,'70'转成了数值70</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//100,null转成了0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Lee'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,Lee转成了NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - 对象 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,如果有toString()或valueOf()</span> 则返回<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>-返回数的值 </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
- 3.乘法
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//7000 </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NaN</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,只要有一个NaN即为NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> * <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> * <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> * -<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">Infinity</span> ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Infinity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//100,true转成数值为1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//0,''转成了0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">null</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//0,null转成了0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Lee'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,Lee转成了NaN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> box = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> * 对象 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//NaN,如果有toString()或valueOf()</span> 则返回<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> - 返回数的值</code>
以上是关于运算符——JavaScript的主要内容,如果未能解决你的问题,请参考以下文章
Code Kata:大整数比较大小&大整数四则运算---加减法 javascript实现