### 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字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

10款趣味橫生的jQuery遊戲插件,讓您的網站更具吸引力,提升用戶粘性!雖然Flash仍然是開發休閒網頁遊戲的最佳軟件,但jQuery也能創造出令人驚喜的效果,雖然無法與純動作Flash遊戲媲美,但在某些情況下,您也能在瀏覽器中獲得意想不到的樂趣。 jQuery井字棋遊戲 遊戲編程的“Hello world”,現在有了jQuery版本。 源碼 jQuery瘋狂填詞遊戲 這是一個填空遊戲,由於不知道單詞的上下文,可能會產生一些古怪的結果。 源碼 jQuery掃雷遊戲

本教程演示瞭如何使用jQuery創建迷人的視差背景效果。 我們將構建一個帶有分層圖像的標題橫幅,從而創造出令人驚嘆的視覺深度。 更新的插件可與JQuery 1.6.4及更高版本一起使用。 下載

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Matter.js是一個用JavaScript編寫的2D剛體物理引擎。此庫可以幫助您輕鬆地在瀏覽器中模擬2D物理。它提供了許多功能,例如創建剛體並為其分配質量、面積或密度等物理屬性的能力。您還可以模擬不同類型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流瀏覽器。此外,它也適用於移動設備,因為它可以檢測觸摸並具有響應能力。所有這些功能都使其值得您投入時間學習如何使用該引擎,因為這樣您就可以輕鬆創建基於物理的2D遊戲或模擬。在本教程中,我將介紹此庫的基礎知識,包括其安裝和用法,並提供一

本文演示瞭如何使用jQuery和ajax自動每5秒自動刷新DIV的內容。 該示例從RSS提要中獲取並顯示了最新的博客文章以及最後的刷新時間戳。 加載圖像是選擇


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。