首页 > web前端 > js教程 > 正文

了解 JavaScript 运算符:带有示例的完整指南

心靈之曲
发布: 2024-12-18 12:03:27
转载
908人浏览过

### javascript 中的运算符

javascript 中的运算符是用于对值和变量执行运算的特殊符号。这些操作可以涉及算术、赋值、比较、逻辑和其他操作。了解运算符对于执行基本计算、比较和控制代码流程至关重要。

javascript 支持多种运算符,它们分为以下类型:


### 1. **算术运算符**
算术运算符用于对数字进行数学计算。

operator description example
addition 5 3 → 8
- subtraction 5 - 3 → 2
* multiplication 5 * 3 → 15
/ division 5 / 3 → 1.666...
% modulus (remainder) 5 % 3 → 2
** exponentiation 5 ** 2 → 25

例子:

let a = 10;
let b = 2;
console.log(a + b);  // output: 12
console.log(a - b);  // output: 8
console.log(a * b);  // output: 20
console.log(a / b);  // output: 5
console.log(a % b);  // output: 0
console.log(a ** b); // output: 100
登录后复制

**

2. 赋值运算符**

赋值运算符用于为变量赋值。

立即学习Java免费学习笔记(深入)”;

operator description example
= assign value x = 5
= add and assign x = 3 → x = x 3
-= subtract and assign x -= 2 → x = x - 2
*= multiply and assign x *= 4 → x = x * 4
/= divide and assign x /= 2 → x = x / 2
%= modulus and assign x %= 3 → x = x % 3
**= exponentiation and assign x **= 2 → x = x ** 2

例子:

let x = 10;
x += 5;  // x = x + 5 -> 15
x *= 2;  // x = x * 2 -> 30
console.log(x);  // output: 30
登录后复制

### 3. **比较运算符**
比较运算符用于比较值并根据条件返回布尔值(true 或 false)。

operator description example
== equal to (loose) 5 == '5' → true
=== equal to (strict) 5 === '5' → false
!= not equal to (loose) 5 != '5' → false
!== not equal to (strict) 5 !== '5' → true
> greater than 5 > 3 → true
less than 5
>= greater than or equal 5 >= 5 → true
less than or equal 5

例子:

console.log(5 == '5');  // output: true (loose comparison)
console.log(5 === '5'); // output: false (strict comparison)
console.log(10 > 5);    // output: true
console.log(3 <= 2);    // output: false
登录后复制

### 4. **逻辑运算符**
逻辑运算符用于执行逻辑运算,返回布尔值。

operator description example
&& logical and true && false → false
` `
! logical not !true → false

#### 示例:

let a = true;
let b = false;
console.log(a && b);  // output: false
console.log(a || b);  // output: true
console.log(!a);      // output: false
登录后复制

### 5. **一元运算符**
一元运算符对单个操作数进行运算以执行特定操作。

operator description example
increment x → x = x 1
-- decrement x-- → x = x - 1
typeof type of operand typeof x → number
void evaluates expression without returning a value void(0)

#### 示例:

let x = 10;
console.log(++x);  // output: 11 (pre-increment)
console.log(x--);  // output: 11 (post-decrement)
console.log(typeof x);  // output: number
登录后复制

### 6. **三元(条件)运算符
**三元运算符是 if...else 语句的简写。它评估一个条件并根据条件是真还是假返回两个值之一。

operator description example
condition ? expr1 : expr2 if condition is true, return expr1; otherwise, return expr2 x > 10 ? 'greater' : 'lesser'

*#### 示例:
*

let age = 18;
let result = age >= 18 ? 'adult' : 'minor';
console.log(result);  // output: adult
登录后复制

### 7. **位运算符
**按位运算符对二进制数进行运算。

operator description example
& and 5 & 3 → 1
` ` or
^ xor 5 ^ 3 → 6
~ not ~5 → -6
left shift 5
>> right shift 5 >> 1 → 2
>>> unsigned right shift 5 >>> 1 → 2

*#### 示例:
*

let a = 5;  // binary: 101
let b = 3;  // binary: 011
console.log(a & b);  // output: 1 (binary: 001)
console.log(a | b);  // output: 7 (binary: 111)
console.log(a << 1); // output: 10 (binary: 1010)
登录后复制

### 8. **扩展运算符 (...)
**扩展运算符允许您将数组或对象中的元素解压到新的数组或对象中。

*#### 示例:
*

let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2);  // Output: [1, 2, 3, 4, 5]

let obj1 = { a: 1, b: 2 };
let obj2 = { ...obj1, c: 3 };
console.log(obj2);  // Output: { a: 1, b: 2, c: 3 }
登录后复制

### 结论

javascript 运算符是执行计算、比较和逻辑运算的基础。无论您是操纵值、比较它们还是控制程序流程,理解这些运算符对于高效编码都至关重要。根据您的任务使用正确的运算符,以确保代码干净且可读。

嗨,我是 abhay singh kathayat!
我是一名全栈开发人员,拥有前端和后端技术方面的专业知识。我使用各种编程语言和框架来构建高效、可扩展且用户友好的应用程序。
请随时通过我的商务电子邮件与我联系:kaashshorts28@gmail.com。

以上就是了解 JavaScript 运算符:带有示例的完整指南的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号