1.表达式

表达式是EMCAscript中的一个“短语”,解释器会通过计算把它转换成一个值。最简单的表达式是字面量或者变量名。

2.前置递增(++box)和后置递增(box++)的区别

看下面一段代码的实现过程:

var box = 100;
alert(box++); // 100  box = box+1;先执行打印box,再赋值,100 + 1 = 101
alert(++box); // 102  box = 1+box;先赋值,再打印box, 101 + 1 = 102
 age = box++;
 height = ++box;
alert(age); //102
alert(height); //104

对比一下是不是这样:

var box = 100;
age = box++;
alert(age); //100
var box = 100;
age = ++box;
alert(age); //101

3.++号的隐含的Number的转换

3.1字符串

var box = '89';
var box1 = 'lc';
alert(typeof box); //string
alert(typeof box1); //string
age = ++box;
age1 = ++box1;
alert(typeof box); //number
alert(typeof box1); //number
alert(typeof age); //number
alert(age1); //NaN
alert(typeof age1); //number

3.2 Boolean

var box1 = false;
box1++; //将false转换成0,0+1=1
alert(box1); //
var box2 = true;
box2++; //将true转换成1, 1+1=2
alert(box2); //2

3.3对象Object

var box= {};
alert(box); //[object object]
box++;
alert(box); //NaN
box = {
	toString:function(){
		return '123'
	}
}
alert(typeof box); //object
box++;
alert(box); //124
alert(typeof box); //number

var box1 = {
	toString :function(){
		return 'lc'
	}
};
box1++;
alert(box1); //NaN
alert(typeof box1); //number

4+号也有这样的功能;但是注意一点

var box = '100';
+box; //这样并不能改变变量,变量也是不能改变,只能消掉重新创建变量。(这是底层原理)
alert(box); //100
alert(typeof box);//stringalert(typeof +box);//number

4.1+号对string的转换

var box = false;
alert(typeof box);//boolean
alert(+box); //0
alert(typeof +box); //number
var box1 = true;
alert(+box1);//1

4.2Object

var box = {};//+box表正数,-box表示负数
alert(box);//[object object]
alert(typeof box); //object
alert(+box); //NaN
alert(typeof +box);//number
var box1 = {
	toString:function(){
		return '123'
	}
};
alert(+box1);//123
alert(typeof +box1); //number

5运算

var box = 1 +NaN;
alert(box);//NaN
var box1 = 100 + '100';
alert(box1);//100100
alert(typeof box1);//string 运算规则是:number和string的运算有一个string,‘+’号就会变string的连字符

5.()括号强制优先级

var age = '年龄:'+10+20;
alert(age);//年龄:1020
var age = '年龄:'+(10+20); //括号强制优先级
alert(age);//年龄:30

6.number 和object

var box = 10 + {};
alert(box); //10[object object]
alert(typeof box);//string 很奇怪

var box = 10 + {
	toString:function(){
		return '20';
	}
};
alert(box);//1020
alert(typeof box);//string
var box = 10 = {
	toString:function(){
		return 20;
	}
};
alert(box);//30
alert(typeof box);//number

 其他的运算转换,以后补充

js的运算的更多相关文章

  1. js赋值运算的理解

    简介 js引擎由于为了效率,很多时候的非直接量赋值都不是copy一份在赋值给新的变量,而是一个引用 ps:直接量:直接值数字字符串等 为什么使用len = doms.length; 里的len效率要比 ...

  2. JS浮点数运算Bug

    JS浮点数运算Bug的解决办法(转) 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.0849999 ...

  3. js浮点数运算需要注意的问题

    最近在js运算浮点数时发现了一个问题.问题是这样的:js函数中处理两个浮点数的相加,为了防止出现0.1+0.2=0.30000000000000004的问题,两个数都先乘以10000后再相加,得到结果 ...

  4. 通过一张简单的图,让你彻底地搞懂JS的==运算

    大家知道,JavaScript中的==是一种比较复杂运算,它的运算规则很奇怪,很容易让人犯错,从而成为JavaScript中“最糟糕的特性”之一. 在仔细阅读ECMAScript规范的基础上,我画了一 ...

  5. 一道JS 连续赋值运算的问题

    原文链接:https://www.cnblogs.com/joesbell/p/6229423.html <script> var a = {n:1}; var b = a; a.x = ...

  6. js浮点数运算封装, 起因财务部分精确计算

    目录 背景 具体代码 背景 项目中用到浮点数,Int 等 js中 Number类型比较多, 加上牵涉到财务软件, 前台js运算等. 有时候会出现精确度的问题 , 公共方法中有好事者写的方法. 此处拿来 ...

  7. 【转】JS浮点数运算Bug的解决办法

    37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两个只有一 ...

  8. JS小数运算失精度的问题

    JS因为是解释性语言,在运算中会有丢失精度的问题,这种现象多出现在浮点型运算的情况下. 例如 5.11 * 100  得到的结果是 511.00000000000006 这种情况尤其是在处理金额的时候 ...

  9. JS位运算和遍历

    JS位运算符 整数 有符号整数:允许使用正数和负数,第32位作为符号位,前31位才是存储位 无符号整数:只允许用正数 如果用n代表位 位数 = 2^n-1 由于位数(1.2.4.8.16...)中只有 ...

  10. js的运算小数点的问题

    问题这样的: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这 ...

随机推荐

  1. StereoBM::disp12MaxDiff Crash the Release

    Initializing "cv::StereoBM bm.state->disp12MaxDiff" should be careful, inappropriate va ...

  2. python的几个常用内置函数

    dir()查看属性(函数和数据对象) help()查看具体的帮助文档 id() 用来查看数据对象的地址 split 分隔(str ---> list): >>> s=" ...

  3. 记sql语句空格带来的问题

    在做分页的时候,引用了一个分页类.在一条sql语句出发生错误,没查出数据,代码如下 $sql="select * from sw_goods".$page->limit; 正 ...

  4. 浅谈Java中的Set、List、Map的区别

    http://developer.51cto.com/art/201309/410205_all.htm

  5. Linux启动过程中几个重要配置文件的执行过程

    Linux 登录后,配置执行顺序为(Debian Serials Capable):/etc/environment -> /etc/profile -> (~/.bash_profile ...

  6. 从eclipse到Android studio/迁移eclipse的Android项目到Android studio平台的注意事项

    整体要注意的地方 先说明一下整体需要注意的地方 1在Android studio建立项目的时候,要注意包名和原来的完全一致,不然会有很多需要改动. 2依赖的jar一定一定要找齐,不然新建项目引用不到, ...

  7. Task Scheduler Error and Success Constants (Windows)

    If an error occurs, the Task Scheduler APIs can return one of the following error codes as an HRESUL ...

  8. CSS兼容性

    1,浮动 ie6中,设置浮动的元素,width自动包裹内容了.通常我们要设定一下这个元素的宽度 子元素浮动父容器高度不能自适应的CSS解决方法 网页前端工作者经常会遇到子元素设置float浮动后导致父 ...

  9. 交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致

    在做交叉报表列头的排序时,遇到这三个问题,下面具体来说一下. 设计的数据库的表结构如图1所示: 图1 要处出来student_name_,s.grade_,s.subject_name_,这三个属性, ...

  10. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...