JavaScript 运算符

x33g5p2x  于2022-04-06 转载在 Java  
字(3.5k)|赞(0)|评价(0)|浏览(240)

在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。

什么是运算符?

在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,

2 + 3; // 5

这里 + 是执行加法的运算符,2 和 3 是运算数。

JavaScript 运算符类型

以下是您将在本教程中学习的不同运算符的列表。

  • 赋值运算符
  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符
  • 字符串运算符
  • 其他运算符
JavaScript 赋值运算符

赋值运算符用于为变量赋值。例如,

const x = 5;

这里,使用 = 运算符将值 5 赋给变量 x 。
    以下是常用赋值运算符的列表:

运算符名称示例
=赋值运算符a = 7; // 7
+=加法赋值运算符a += 5; // a = a + 5
-=减法赋值运算符a -= 2; // a = a - 2
*=乘法赋值运算符a *= 3; // a = a * 3
/=除法赋值运算符a /= 2; // a = a / 2
%=余数赋值运算符a %= 2; // a = a % 2
**=幂赋值运算符a = 2; // a = a2

注意:常用的赋值运算符是 = 。一旦我们学习算术运算符,您将了解其他赋值运算符,如 +=、-=、*= 等。

JavaScript 算术运算符

算术运算符用于执行算术计算。例如,

const number = 3 + 5; // 8

这里,+ 运算符用于添加两个运算数。

运算符名称示例
+x + y
-x - y
*x * y
/x / y
%余数x % y
++自增(自增1)x 或 x
自减(自减1)–x 或 x–
**x ** y
示例 1:JavaScript 中的算术运算符
let x = 5;
let y = 3;

// addition
console.log('x + y = ', x + y);  // 8

// subtraction
console.log('x - y = ', x - y);  // 2

// multiplication
console.log('x * y = ', x * y);  // 15

// division
console.log('x / y = ', x / y);  // 1.6666666666666667

// remainder
console.log('x % y = ', x % y);   // 2

// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x);     // 7

// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x);     // 5

//exponentiation
console.log('x ** y =', x ** y);

访问 Increment ++ and Decrement – Operator as Prefix and Postfix 以了解更多信息。
    注意:** 运算符是在 ECMAScript 2016 中引入的,某些浏览器可能不支持它们。要了解更多信息,请访问 JavaScript exponentiation browser support

JavaScript 比较运算符

比较运算符比较两个值并返回一个布尔值,要么 true 要么 false 。例如,

const a = 3, b = 2;
console.log(a > b); // true

这里,比较运算符 > 用于比较是否 a 大于 b 。

运算符描述示例
==等于:如果操作数相等则返回 truex == y
!=不等于:如果操作数不相等则返回 truex != y
===严格等于:如果操作数相等且类型相同则返回 truex === y
!==严格不等于:如果操作数相等但类型不同或根本不相等,则返回 truex !== y
>大于:如果左操作数大于右操作数,则返回 truex > y
>=大于或等于:如果左操作数大于或等于右操作数,则返回 truex >= y
<小于:如果左操作数小于右操作数,则返回 truex < y
<=小于或等于:如果左操作数小于或等于右操作数,则返回 truex <= y
示例 2:JavaScript 中的比较运算符
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false

比较运算符用于决策和循环。您将在后面的教程中详细了解比较运算符的使用。

JavaScript 逻辑运算符

逻辑运算符执行逻辑运算并返回一个布尔值,要么 true 要么 false 。例如,

const x = 5, y = 3;
(x < 6) && (y < 5); // true

这里,&& 是逻辑运算符 AND。因为 x < 6 和 y < 5 都是 true,所以结果是 true。

运算符描述示例
&&逻辑与:如果两个操作数都是true,返回 true;否则返回 falsex && y
||逻辑或:如果任一操作数是 true,返回 true;如果两者都是 false,则返回 falsex || y
!逻辑非:如果操作数是false,返回 true;反之亦然!x
示例 3:JavaScript 中的逻辑运算符
// logical AND
console.log(true && true); // true
console.log(true && false); // false

// logical OR
console.log(true || false); // true

// logical NOT
console.log(!true); // false

输出:

true
false
true
false

逻辑运算符用于决策和循环。您将在后面的教程中详细了解逻辑运算符的使用。

JavaScript 位运算符
运算符描述
&按位与
|按位或
^按位异或
~按位非
<<左移
>>右移
>>>零填充右移

位运算符在日常编程中很少使用。如果您有兴趣,请访问 JavaScript Bitwise Operators 以了解更多信息。

JavaScript 字符串运算符

在 JavaScript 中,您还可以使用 + 运算符连接(join)两个或多个字符串。

示例 4:JavaScript 中的字符串运算符
// concatenation operator
console.log('hello' + 'world');

let a = 'JavaScript';

a += ' tutorial';  // a = a + ' tutorial';
console.log(a);

输出:

helloworld
JavaScript tutorial
其他 JavaScript 运算符

这是 JavaScript 中可用的其他运算符的列表。您将在后面的教程中了解这些运算符。

运算符描述示例
,计算多个操作数并返回最后一个操作数的值。let a = (1, 3 , 4); // 4
?:根据条件返回值(5 > 3) ? ‘success’ : ‘error’; // “success”
delete删除对象的属性或数组的元素delete x
typeof返回一个指示数据类型的字符串typeof 3; // “number”
void丢弃表达式的返回值void(x)
in如果指定的属性在对象中,则返回 trueprop in object
instanceof如果指定对象属于指定对象类型,则返回object instanceof object_type

上一教程 :JS Data Types                                         下一教程 :JS Comments

参考文档

[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/operators

相关文章