Javascript Operators in Hindi
अन्य programming language की तरह javascript में भी operators होतें हैं | arithmetic operation करना या फिर variable में value assign करना या फिर दो values में comparison करना ये सब काम Operators के जरिये किया जाता है |
Types of Javascript Operators
javascript में कई तरह के operators हैं और वो हैं
- Arithmetic
- Comparison
- Logical
- Assignment
- Conditional
Arithmetic Operators
operands के बिच में mathmetical operation करने के लिए arithmetic operators का इस्तेमाल होता है |
उदाहरण के लिए दो variables लेते हैं a और b | let a= 100; let b =10;
ऑपरेटर्स | नाम | विवरण |
+ | addition | ये दो या अधिक operands की addition करता है उदाहरण : a + b // result : 110 |
– | substraction | ये पेहले operand से दुसरे operand को substract करता है उदाहरण : a – b // result : 90 |
* | multiplication | operands को गुणा(multiply) करता है उदाहरण : a * b // result : 1000 |
/ | division | पहले operand से दुसरे को भाग(divide) करता है | उदाहरण : a / b //result :10 |
% | modulo | इस operand से reminder निकाला जाता है उदाहरण : a % b //result : 0 |
++ | increment | इससे operand की value, 1 value से बढती है | उदाहरण : a ++ //result : 101 |
– – | decrement | इससे operand की value, 1 value से घटती है | उदाहरण : a – – //result : 99 |
**नॉटपॉइंट अगर + operator दो integer values के ली गयी हो तो ये addition करता है | पर अगर एक भी operand string हुआ तो ये concatenation करता है | concatenation में सारे values को एक साथ मिलाकर दिखता है |
उदाहरण
let a = 10; let b=20; let c="apple"; var res= a+b; //output-> 30 res = a+b+c //output-> 1020apple
Comparison Operators
comparison operator left side की operand से right side की operand को compare करती है और result में true या false बताती है | comparison operators का उपयोग if statements में होता है जिससे फिर अलग अलग condition लगाने में मदद मिलती है |
comparison operators list:
ऑपरेटर्स | नाम | विवरण |
== | comparison without type | left वाली value right value से equal है या नहीं | अगर value equal हुई तो result true आएगी अगर नहीं, तो false return होगा | |
=== | comparison with type | left operand को right operand से equal है या नहीं वो check किया जाता है और साथ ही साथ दोनों operands की datatype equal है या नहीं वो देखा जाता है | |
< | less than | इससे left operand की value right operand की value से कम है या नहीं वो चेक किया जाता है | |
> | greater than | इससे left operand की value right operand की value से ज्यादा है या नहीं वो चेक किया जाता है | |
<= | less than equal to | left operand की value right operand की value से कम है या बराबर है वो बताता है | |
>= | greater than equal to | left operand की value right operand की value से ज्यादा है या बराबर है वो बताता है | |
!= | not equal to | left operand की value right operand की value से equal नहीं हो तो result true आएगी नहीं तो result false मिलेगी | |
!== | not equal to with datatype | left operand की value right operand की value से equal भी ना हो और दोनों operands की data type भी एक जैसी ना हो | तभी result true आएगी | |
**नॉटपॉइंट !(not) का इस्तेमाल reverse result देने के लिए होता है | !=(not equal) =(equal) का उल्टा और !==(not equal with type) ==(equal with type) का उल्टा काम करता है |
comparison operators example
<!doctype html> <html> <head> <title>javascript comparison operator</title> <script type="text/javascript"> var a = 10; var b = 20; var c = "20"; var linebreak = "<br />"; document.write("a = " + a + ", "); document.write("b = " + b + ", "); document.write("c = '" + c +"'"); document.write(linebreak); document.write("(a > b) result is "); document.write(a > b); document.write(linebreak); document.write("(a < b) result is "); document.write(a < b); document.write(linebreak); document.write("(a >= b) result is "); document.write(a >= b); document.write(linebreak); document.write("(a <= b) result is "); document.write(a <= b); document.write(linebreak); document.write("(a == c) result is "); result = (a == c); document.write(result); document.write(linebreak); document.write("(a === c) result is "); document.write(a === c); document.write(linebreak); document.write("(a != c) result is "); document.write(a != b); document.write(linebreak); document.write("(a !== c) result is "); document.write(a == b); document.write(linebreak); </script> </head> <body> </body> </html>
result
Logical Operators
assignment operators एक से ज्यादा conditions को चेक करने के लिए इस्तेमाल होती है | if statements या फिर loop में दो या अधिक conditions होतें हैं | तो सारे conditions true हैं या false हैं, उसके हिसाब से loop के अंदर की statement run होती है | और वही conditions check करने के लिए logical operators लिया जाता है |
Logical operators list
ऑपरेटर्स | नाम | विवरण |
&& | and opeartor | अगर सारे conditions true हुए तभी result true होगा नहीं तो ये false return करेगा | |
|| | or operator | अगर सारे conditions में से, एक भी condition true हुआ तभी result true होगा | |
! | not operator | ये condition का उल्टा result देता है | अगर statement true है तो result false आएगा | |
and operator condition
condition1 | condition2 | result |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
उदाहरण
<script type="text/javascript"> var a=10; var res = a>8 && a<15; // both conditions are true document.write(res); // output -> true </script>
or operator condition
condition1 | condition2 | result |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
उदाहरण
<script type="text/javascript"> var a=10; var res = a>18 || a<15; // 1st condition false, second is true document.write(res); // output -> true </script>
not operator
condition | result |
true | false |
false | true |
उदाहरण
<script type="text/javascript"> var a=10; var b=5; document.write(a!=b); // output -> true </script>
Assignment Operators
assignment operators variables या constant को value assign करने के लिए इस्तेमाल किया जाता है |
assignment operators list
operators | shorthand | equivalent expression | description |
= | x=y | x=y | y की value x को assign करना |
+= | x+=y | x=x+y | x और y की value को जोड़ कर पूरी value x को assign करना |
-= | x-=y | x=x-y | x से y की value को घटा कर परिणाम x को assign करना |
*= | x*=y | x=x*y | x को y से गुणा करके x को assign करना |
/= | x/=y | x=x/y | x से y को divide करके परिणाम x को assign करना |
%= | x%=y | x=x%y | x से y को divide करके आया हुआ reminder x को assign करना |
Conditional Operator
Conditional Operator को ternary operator भी कहा जाता है | ये if else condition का shorthand है | अगर सिर्फ if और else condition लगाना है तो उसे एक ही line में ternary operator के जरिये बताया जा सकता है |
इसमें फर्क ये है की condition और statement को एक ही line में लिख सकते हैं | इसकी वजह से unnecessary अधिक coding की जरुरत नहीं पड़ती | पर इस operators के जरिये दो से अधिक conditions नहीं लिख सकते |
ternary operator syntax
(condition) ? statement1 : statement2;
इस operator में पेहले condition बताई जाती है | अगर condition true हुयी तो statement जिसे ? mark और :(colon) के बिच लिखी गयी है वो run करेगा | अगर condition false हुयी तो statement जिसे :(colon) और ;(semicolon) के बिच में लिखी गयी है वो run करेगा |
conditional operator में पेहली statement true condition के लिए होता है और दूसरा statement false condition के लिए होता है |
उदाहरण
<script type="text/javascript"> var a=20; var res; res = (a>10) ? 'true' : res = 'false'; document.write(res); </script>