• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

2周前 (04-29) 8次浏览

# 自增练习、逻辑运算符

## 题目与解答

var n1 = 10;

var n2 = 20;

var n = n1++;

console.log( “n =”+n); 10

console.log(“n1 = “+n1); 11

n = ++n1;

console.log(“n = +n”); 12

console.log(“n1 = “+n1);12

n = n2–;

console.log(“n = “+n); 20

console.log(“n2 = “+n2); 19

n = –n2;

console.log(“n = “+n); 18

cosnole.log(“n2 = “+n2); 18

## 逻辑运算符

JS中为我们提供了三种逻辑运算符

### ！非

``````	可以用！来对一个值进行非运算
``````

​ 非运算就是对一个布尔值进行取反操作；

​ true变false， false变true；

``````<head>
<script>
var a = false;
a = !a;
console.log("a="+a);
</script>

``````

``````<head>
<script>
var a = false;
a = !!a;
console.log("a="+a);
</script>
``````

``````	var b = 10;
b = !b;
console.log("b="+b);
console.log(typeof b);
``````

``````	var b = 10;
b = ！!b;
console.log("b="+b);
console.log(typeof b);
``````

### &&与

&&可以对符号两侧的值进行与运算并返回结果

``````	var a = true && true;
console.log(a);
``````

``````var a = true && true;
a = true && false;
a = false && true;
a = false && false;
``````

JS中的“与”属于短路的，如果第一个是false则不会看第二个值

``````		true && alert("hello world");
``````

``````false && alert("hello world")
``````

### ||或

||可以对两侧的值进行或运算并返回结果

``````		a = false || false;
console.log(a);
``````

``````		a = false || true;
console.log(a);
``````

## 总结&&是找flase ||是找true

``````true || alert(heelo);
``````

``````flase ||alert(hello)；
``````