JavaScript逻辑运算符与赋值运算符

逻辑运算符

JavaScript中有三个逻辑运算符,&&与、||或、!非

JavaScript 中的逻辑运算符可用来确定变量或者是值之间的逻辑关系。通常用于布尔型值,会返回一个布尔值true 或 false。而 &&、|| 运算符能够使用非布尔值的操作数,此时会返回一个非布尔型值。

1. 逻辑与 &&

js中逻辑与和其他语言不太一样,如果第一个操作数是true(或者能够转为true),计算结果就是第二个操作数,如果第一个操作数是false,结果就是false(短路计算),对于一些特殊数值不遵循以上规则.

<script type="text/javascript">
var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(true && 10);//10
console.log(false && b);//flase
console.log(100 && false);//flase
console.log(undefined && false);//undefined
console.log(NaN && false);//NaN
console.log(null && false);//null
console.log('' && false);//空串
console.log(0 && 100);//0
console.log(5 && 100);//100
console.log(a && b);//hello
console.log(obj && 200);//200
</script>

2. 逻辑或 ||

如果第一个操作数不是false,结果就是第一个操作数,否则结果是第二个操作数。如果第一个操作数能够转为true,结果就是第一个操作数。

<script type="text/javascript">
var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(true || 10);//true
console.log(false || b);//b
console.log(100 || false);//100
console.log(undefined || 9);//9
console.log(NaN || false);//第一个操作数是NaN转false,结果第二个操作数
console.log(null || a);//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
</script>

3. 逻辑非

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

<script type="text/javascript">
var a = [1,2,3];
var b = "hello";
var obj = new Object();
var d;

console.log(!"");
console.log(!d);
console.log(!a);
console.log(!b);
console.log(!obj);
</script>

赋值运算符

基本的赋值运算符是等号(=),该运算符把它右边的运算值赋给左边。即,x = y 把 y 的值赋给 x。 其他的赋值运算符通常是标准运算符的简写形式。

1. 赋值

简单的赋值运算符,把一个值赋给一个变量。为了把一个值赋给多个变量,可以以链式使用赋值运算符。

语法:

Operator: x = y

例:

// Assuming the following variables
//  x = 5
//  y = 10
//  z = 25

x = y     // x is 10
x = y = z // x, y and z are all 25

2. 加赋值

加赋值运算符把一个右值与一个变量相加,然后把相加的结果赋给该变量。两个操作数的类型决定了加赋值运算符的行为。

语法:

Operator: x += y
Meaning:  x  = x + y

例:

// 定义下列变量
//  foo = 'foo'
//  bar = 5
//  baz = true

// Number + Number -> addition
bar += 2 // 7

// Boolean + Number -> addition
baz += 1 // 2

// Boolean + Boolean -> addition
baz += false // 1

// Number + String -> concatenation
bar += 'foo' // "5foo"

// String + Boolean -> concatenation
foo += false // "foofalse"

// String + String -> concatenation
foo += 'bar' // "foobar"

3. 减赋值

减赋值运算符使一个变量减去右值,然后把结果赋给该变量。

语法:

Operator: x -= y
Meaning:  x  = x - y

例:

// 假定已定义了下面的变量
//  bar = 5

bar -= 2     // 3
bar -= "foo" // NaN

4. 乘赋值

乘赋值运算符使一个变量乘以右值,然后把相成的结果赋给该变量。

语法:

Operator: x *= y
Meaning:  x  = x * y

例:

// 假定已定义了下面的变量
//  bar = 5

bar *= 2     // 10
bar *= 'foo' // NaN

5. 除赋值

除赋值运算符使一个变量除以右值,然后把结果赋给该变量。

语法:

Operator: x /= y
Meaning:  x  = x / y

例:

// 假定已定义了下面的变量
//  bar = 5

bar /= 2     // 2.5
bar /= "foo" // NaN
bar /= 0     // Infinity

6. 模赋值

模赋值运算符使一个变量除以右值,然后把余数赋给该变量。

语法:

Operator: x %= y
Meaning:  x  = x % y

例:

// Assuming the following variable
//  bar = 5

bar %= 2     // 1
bar %= 'foo' // NaN
bar %= 0     // NaN

7. 指数赋值

指数赋值运算符使一个变量为底数、以右值为指数的指数运算(乘方)结果赋给该变量。

语法:

Operator: x **= y
Meaning:  x  = x ** y

例:

// Assuming the following variable
//  bar = 5

bar **= 2     // 25
bar **= 'foo' // NaN

8. 左移赋值

左移赋值运算符使变量向左移动指定位数的比特位,然后把结果赋给该变量。

语法:

Operator: x <<= y
Meaning:  x   = x << y

例:

var bar = 5; //  (00000000000000000000000000000101)
bar <<= 2; // 20 (00000000000000000000000000010100)

9. 右移赋值

右移赋值运算符使变量向右移指定位数的比特位,然后把结果赋给该变量。

语法:

Operator: x >>= y
Meaning:  x   = x >> y

例:

var bar = 5; //   (00000000000000000000000000000101)
bar >>= 2;   // 1 (00000000000000000000000000000001)

var bar = -5; //    (-00000000000000000000000000000101)
bar >>= 2;  // -2 (-00000000000000000000000000000010)

10. 无符号右移赋值

无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量。

语法:

Operator: x >>>= y
Meaning:  x    = x >>> y

例:

var bar = 5; //   (00000000000000000000000000000101)
bar >>>= 2;  // 1 (00000000000000000000000000000001)

var bar = -5; // (-00000000000000000000000000000101)
bar >>>= 2; // 1073741822 (00111111111111111111111111111110)

11. 按位与赋值

按位与赋值运算符使用两个操作值的二进制表示,执行按位与运算,并把结果赋给变量。

语法:

Operator: x &= y
Meaning:  x  = x & y

例:

var bar = 5;
// 5:     00000000000000000000000000000101
// 2:     00000000000000000000000000000010
bar &= 2; // 0

12. 按位异或赋值

按位异或赋值运算符使用两个操作值的二进制表示,执行二进制异或运算,并把结果赋给变量。

语法:

Operator: x ^= y
Meaning:  x  = x ^ y

例:

var bar = 5;
bar ^= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111

13. 按位或赋值

按位或赋值运算符使用两个操作值的二进制表示,执行按位或运算,并把结果赋给变量。

语法:

Operator: x |= y
Meaning:  x  = x | y

例:

var bar = 5;
bar |= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111

猜你喜欢

转载自blog.csdn.net/QIANDXX/article/details/114176536