### 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. 赋值运算符**
赋值运算符用于为变量赋值。
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 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
### 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 |
例子:
let x = 10; x += 5; // x = x + 5 -> 15 x *= 2; // x = x * 2 -> 30 console.log(x); // Output: 30
### 4. **逻辑运算符**
逻辑运算符用于执行逻辑运算,返回布尔值。
Operator | Description | Example |
---|---|---|
&& | Logical AND | true && false → false |
` | ` | |
! | Logical NOT | !true → false |
#### 示例:
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 <hr> <p><strong>### 5. **一元运算符</strong>**<br> 一元运算符对单个操作数进行运算以执行特定操作。</p> <div><table> <thead> <tr> <th>Operator</th> <th>Description</th> <th>Example</th> </tr> </thead> <tbody> <tr> <td> </td> <td>Increment</td> <td> x → x = x 1 </td> </tr> <tr> <td>--</td> <td>Decrement</td> <td> x-- → x = x - 1 </td> </tr> <tr> <td>typeof</td> <td>Type of operand</td> <td> typeof x → number </td> </tr> <tr> <td>void</td> <td>Evaluates expression without returning a value</td> <td>void(0)</td> </tr> </tbody> </table></div> <p><strong>#### 示例:</strong><br> </p> <pre class="brush:php;toolbar:false">let a = true; let b = false; console.log(a && b); // Output: false console.log(a || b); // Output: true console.log(!a); // Output: false
### 6. **三元(条件)运算符
**三元运算符是 if...else 语句的简写。它评估一个条件并根据条件是真还是假返回两个值之一。
Operator | Description | Example |
---|---|---|
condition ? expr1 : expr2 | If condition is true, return expr1; otherwise, return expr2 | x > 10 ? 'Greater' : 'Lesser' |
*#### 示例:
*
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
### 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 x = 10; x += 5; // x = x + 5 -> 15 x *= 2; // x = x * 2 -> 30 console.log(x); // Output: 30
### 8. **扩展运算符 (...)
**扩展运算符允许您将数组或对象中的元素解压到新的数组或对象中。
*#### 示例:
*
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 <hr> <p><strong>### 结论</strong></p> <p>JavaScript 运算符是执行计算、比较和逻辑运算的基础。无论您是操作值、比较它们还是控制程序流程,理解这些运算符对于高效编码都至关重要。根据您的任务使用正确的运算符,以确保代码干净且可读。</p> <p>嗨,我是 Abhay Singh Kathayat!<br> 我是一名全栈开发人员,拥有前端和后端技术方面的专业知识。我使用各种编程语言和框架来构建高效、可扩展且用户友好的应用程序。<br> 请随时通过我的商务电子邮件与我联系:kaashshorts28@gmail.com。</p>
以上是了解 JavaScript 运算符:带有示例的完整指南的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用