JavaScript Operators
JavaScript में operators का उपयोग values पर कोई operation (क्रिया) करने के लिए किया जाता है — जैसे जोड़ना, घटाना, तुलना करना, या किसी logic को लागू करना।
इस chapter में आप सीखेंगे:
- Operators क्या होते हैं
- उनके प्रकार (Types of Operators)
- हर operator के उदाहरण और उपयोग
- अभ्यास प्रश्न
JavaScript Operators के मुख्य प्रकार
Operator Type | Description |
---|---|
Arithmetic Operators | गणनाएँ जैसे जोड़, घटाव, गुणा, भाग |
Assignment Operators | किसी value को assign करने के लिए |
Comparison Operators | दो values की तुलना के लिए |
Logical Operators | कई conditions को जोड़ने के लिए |
String Operators | Strings को जोड़ने (concatenate) के लिए |
Unary / Type Operators | एक ही operand पर काम करते हैं |
Ternary Operator | Short form में condition check करने के लिए |
Bitwise Operators | Binary level पर operations |
1. Arithmetic Operators
ये operators संख्याओं पर गणना करने के लिए होते हैं:
Operator | Meaning | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 10 / 2 | 5 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | x++ | बढ़ाए 1 |
-- | Decrement | x-- | घटाए 1 |
2. Assignment Operators
किसी variable में value assign करने के लिए:
Operator | Meaning | Example | Same As |
---|---|---|---|
= | Assign | 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 %= 2 | x = x % 2 |
3. Comparison Operators
दो values की तुलना करके Boolean (true/false) result देते हैं:
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to (value) | 5 == '5' | true |
=== | Equal to (value + type) | 5 === '5' | false |
!= | Not equal to (value) | 4 != 3 | true |
!== | Not equal to (value + type) | 4 !== '4' | true |
> | Greater than | 7 > 5 | true |
< | Less than | 3 < 4 | true |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 4 <= 6 | true |
4. Logical Operators
कई conditions को जोड़ने या check करने के लिए:
Operator | Name | Example | Result |
---|---|---|---|
&& | AND | true && true | true |
` | ` | OR | |
! | NOT | !true | false |
5. String Operators
Strings को जोड़ने के लिए +
का उपयोग किया जाता है:
let first = "Hello ";
let second = "World!";
let result = first + second;
console.log(result); // Output: Hello World!
6. Type & Unary Operators
🔸 typeof
Variable की data type को बताता है:
typeof "Hello" // "string"
typeof 123 // "number"
typeof true // "boolean"
🔸 Unary -
, +
let x = "5";
let y = +x; // Converts string to number
7. Ternary Operator
Condition को short form में check करने के लिए:
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // Output: Adult
8. Bitwise Operators (Advanced)
Binary level पर operations के लिए — आमतौर पर rarely use होते हैं।
Operator | Name |
---|---|
& | AND |
` | ` |
^ | XOR |
~ | NOT |
<< | Left shift |
>> | Right shift |
अभ्यास प्रश्न
==
और===
में क्या अंतर है?- नीचे दिए गए expression का result क्या होगा?
let x = 5; x += 3; console.log(x);
typeof null
का result क्या आता है?- Ternary operator का syntax क्या होता है?
- नीचे दिए गए code का output बताइए:
let a = "10"; let b = 10; console.log(a == b); // ? console.log(a === b); // ?