JavaScript学习,常用常记(运算符,比较和逻辑运算符)

版权声明:转载请附上此博客地址,谢谢 https://blog.csdn.net/qq_30796379/article/details/88987603

JavaScript 算术运算符

y=5,下面的表格解释了这些算术运算符:

运算符 描述 例子 x 运算结果 y 运算结果 在线实例
+ 加法 x=y+2 7 5 实例 »
- 减法 x=y-2 3 5 实例 »
* 乘法 x=y*2 10 5 实例 »
/ 除法 x=y/2 2.5 5 实例 »
% 取模(余数) x=y%2 1 5 实例 »
++ 自增 x=++y 6 6 实例 »
x=y++ 5 6 实例 »
-- 自减 x=--y 4 4 实例 »
x=y-- 5 4 实例 »

JavaScript 赋值运算符

+=,-=,*=,/=,%=

用于字符串的 + 运算符

  • + 运算符用于把文本值或字符串变量加起来(连接起来)。
  • 如需把两个或多个字符串变量连接起来,请使用 + 运算符。

对字符串和数字进行加法运算

两个数字相加,返回数字相加的和,如果数字与字符串相加,返回字符串;

var result1=5+5+"abc"; //结果将是"10abc"
var result2= ""+5+5+"abc"; //结果将是"55abc"

数字和布尔值相加,布尔值 false 转成 0,true 转成 1;

字符串与布尔值相加,布尔值转化成字符串;

取模运算的结果符号只与左边值的符号有关:

var x = 7 % 3; // 结果为 1
var y = 7 % (-3); // 结果为 1
var z = (-7) % 3; // 结果为 -1

数字与 null(空值) 相加,null 转化为数字 0;

字符串与 null(空值) 相加,null 转化为字符串;

var car=null+3+4;    // 结果为7
var car=null+"a";    // 结果为 nulla

比较运算符

比较运算符在逻辑语句中使用,以测定变量或值是否相等。

x=5,下面的表格解释了比较运算符:

运算符 描述 比较 返回值 实例
== 等于 x==8 false 实例 »
x==5 true 实例 »
=== 绝对等于(值和类型均相等) x==="5" false 实例 »
x===5 true 实例 »
!=  不等于 x!=8 true 实例 »
!==  不绝对等于(值和类型有一个不相等,或两个都不相等) x!=="5" true 实例 »
x!==5 false 实例 »
>  大于 x>8 false 实例 »
<  小于 x<8 true 实例 »
>=  大于或等于 x>=8 false 实例 »
<=  小于或等于 x<=8 true 实例 »

逻辑运算符

逻辑运算符用于测定变量或值之间的逻辑。

给定 x=6 以及 y=3,下表解释了逻辑运算符:

运算符 描述 例子
&& and (x < 10 && y > 1) 为 true
|| or (x==5 || y==5) 为 false
! not !(x==y) 为 true

条件运算符

JavaScript 还包含了基于某些条件对变量进行赋值的条件运算符。

<!DOCTYPE html>
<html>
<head> 
	<meta charset="utf-8"> 
	<title>菜鸟教程(runoob.com)</title> 
</head>
	
<body>
	<p>点击按钮检测年龄。</p>
	年龄:<input id="age" value="18" />
	<p>是否达到投票年龄?</p>
	<button onclick="myFunction()">点击按钮</button>
	<p id="demo"></p>
	<script>
		function myFunction(){
			var age,voteable;
			age=document.getElementById("age").value;
			voteable=(age<18)?"年龄太小":"年龄已达到";
			document.getElementById("demo").innerHTML=voteable;
		}
	</script>
</body>
</html>
0||3 : 3

1||3 : 1

0&&3 : 0

1&&3 : 3

转换为布尔类型的规则: null、undefined、0、NaN、空字符串转换为false,其他转化为 true。

1. 取反 !

首先把数据转化为布尔值,然后取反,结果为 true 或 false。

var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(!"");   //true
console.log(!d);    //true
console.log(!a);    //false
console.log(!b);    //false
console.log(!obj);  //false

2. 逻辑与 &&

JavaScript 中逻辑与和其他语言不太一样,如果第一个操作数是 true(或者能够转为 true),计算结果就是第二个操作数,如果第一个操作数是 false,结果就是 false(短路计算),对于一些特殊数值不遵循以上规则。(个人理解为:如果运算的第一个操作数为true,则返回第二个操作数,反之则返回第一个操作数)

var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(true && 10);            //第一个操作数是true,结果是第二个操作,也就是10
console.log(false && b);            //第一个操作数是false,结果flase
console.log(100 && false);          //第一个操作数是100,结果flase
console.log(undefined && false);    //第一个操作数是undefined,结果undefined
console.log(NaN && false);          //第一个操作数是NaN,结果NaN
console.log(null && false);         //第一个操作数是null,结果null
console.log('' && false);           //第一个操作数是空串,结果空串
console.log(0 && 100);              //结果是0
console.log(5 && 100);              //100
console.log(a && b);                //hello
console.log(obj && 200);            //200

3. 逻辑或 ||

如果第一个操作数不是 false,结果就是第一个操作数,否则结果是第二个操作数。如果第一个操作数能够转为 true,结果就是第一个操作数(个人理解为:如果运算的第一个操作数为 true,则返回第一个操作数,反之则返回第二个操作数)

var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(true || 10);        //第一个操作数是true,结果是第一个操作,也就是true
console.log(false || b);        //第一个操作数是false,结果是第二个操作数b
console.log(100 || false);      //第一个操作数是100,结果100
console.log(undefined || 9);    //第一个操作数是undefined转false,结果9
console.log(NaN || false);      //第一个操作数是NaN转false,结果第二个操作数
console.log(null || a);         //第一个操作数是null转false,结果a
console.log('' || false);       //第一个操作数是空串转false,结果第二操作数
console.log(0 || 100);          //结果是100
console.log(5 || 100);          //5
console.log(a || b);            //a
console.log(obj || 200);        //obj

猜你喜欢

转载自blog.csdn.net/qq_30796379/article/details/88987603