6.JavaScript-运算符注意点

12 篇文章 0 订阅

运算符注意点

1.和C语言不同的是JavaScript中整数除以整数结果是小数;

	var res4 = 10 / 3;
    console.log(res4); // 3.3333

2.任何值和NaN做运算都得NaN;

	var result = 2 + NaN;
	console.log(result); //NaN

3.非Number类型的值进行运算时,会将这些值转换为Number然后再运算;

	var result = true + 1; // + - * /  %
	console.log(result); // 2
	result = true + false;
	console.log(result); // 1
	result = 2 + null; 
	console.log(result);// 2

4.任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作;

	var result = 1 + "123";
	console.log(result); // 1123
	result = 2 + "true";
	console.log(result); // 2true

5.任何的值和字符串做- * / %法运算, 都会先转换为字符串转换为Number;

	var result = 2 - "1"; // - * /  %
	console.log(result); // 1
	result = "2" - "1";
	console.log(result); // 1

6.取余运算m%n, n等于0 返回NaN, 其它和C语言一样

	var result = 10 % 0;
	console.log(result); // NaN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值