जावास्क्रिप्ट ऑपरेटर (उदाहरण के साथ)

इस ट्यूटोरियल में, आप जावास्क्रिप्ट में उपलब्ध विभिन्न ऑपरेटरों के बारे में और उदाहरणों की मदद से उनका उपयोग कैसे करें, इसके बारे में जानेंगे।

एक ऑपरेटर क्या है?

जावास्क्रिप्ट में, एक ऑपरेटर एक विशेष प्रतीक होता है जिसका उपयोग ऑपरेंड (मान और चर) पर संचालन करने के लिए किया जाता है। उदाहरण के लिए,

 2 + 3; // 5

यहाँ +एक ऑपरेटर है कि इसके प्रदर्शन करती है, और 2और 3ऑपरेंड हैं।

जावास्क्रिप्ट ऑपरेटर प्रकार

यहाँ विभिन्न ऑपरेटरों की एक सूची है जो आप इस ट्यूटोरियल में सीखेंगे।

  • असाइनमेंट ऑपरेटर्स
  • अंकगणितीय आपरेटर
  • तुलना संचालक
  • लॉजिकल ऑपरेटर्स
  • बिटवाइज ऑपरेटर्स
  • स्ट्रिंग ऑपरेटर्स
  • अन्य संचालक

जावास्क्रिप्ट असाइनमेंट ऑपरेटर्स

असाइनमेंट ऑपरेटर्स का उपयोग वैरिएबल को मान असाइन करने के लिए किया जाता है । उदाहरण के लिए,

 const x = 5;

यहां, =ऑपरेटर का उपयोग 5वैरिएबल को मान देने के लिए किया जाता है x

यहां आमतौर पर उपयोग किए जाने वाले असाइनमेंट ऑपरेटरों की सूची दी गई है:

ऑपरेटर नाम उदाहरण
= असाइनमेंट ऑपरेटर a = 7; // 7
+= जोड़ का काम a += 5; // a = a + 5
-= घटाव असाइनमेंट a -= 2; // a = a - 2
*= गुणन निरूपण a *= 3; // a = a * 3
/= डिवीजन असाइनमेंट a /= 2; // a = a / 2
%= रिमाइंडर असाइनमेंट a %= 2; // a = a % 2
**= घातांक असाइनमेंट a **= 2; // a = a**2

नोट: आमतौर पर इस्तेमाल किया जाने वाला असाइनमेंट ऑपरेटर है =। आप इस तरह के रूप में अन्य काम ऑपरेटरों समझ जाएगा +=, -=, *=आदि एक बार हम अंकगणितीय ऑपरेटर सीखते हैं।

जावास्क्रिप्ट अंकगणितीय ऑपरेटर

अंकगणित ऑपरेटरों का उपयोग अंकगणितीय गणना करने के लिए किया जाता है । उदाहरण के लिए,

 const number = 3 + 5; // 8

यहां, +ऑपरेटर का उपयोग दो ऑपरेंड को जोड़ने के लिए किया जाता है।

ऑपरेटर नाम उदाहरण
+ जोड़ x + y
- घटाव x - y
* गुणन x * y
/ विभाजन x / y
% अवशेष x % y
++ वृद्धि (1 से वृद्धि) ++x या x++
-- कमी (1 से घटाकर) --x या x--
** घातांक (पावर) x ** y

उदाहरण 1: जावास्क्रिप्ट में अंकगणितीय ऑपरेटर

 let x = 5; let y = 3; // addition console.log('x + y = ', x + y); // subtraction console.log('x - y = ', x - y); // multiplication console.log('x * y = ', x * y); // division console.log('x / y = ', x / y); // remainder console.log('x % y = ', x % y); // increment console.log('++x = ', ++x); // x is now 6 console.log('x++ = ', x++); // x returns 6 and then increases by 1 console.log('x = ', x); // decrement console.log('--x = ', --x); // x is now 6 console.log('x-- = ', x--); // x returns 6 and then increases by 1 console.log('x = ', x); //exponentiation console.log('x ** y =', x ** y);

अधिक जानने के लिए ++ और - ऑपरेटर पर जाएँ।

आउटपुट

 x + y = 8 x - y = 2 x * y = 15 x / y = 1.666666666666666667 x% y = 2 ++ x = 6 x ++ = 6 x = 7 --x = 6 x-- = 6 x = 5 x ** y = 125

नोट : ** ऑपरेटर को EcmaScript 2016 में पेश किया गया था और कुछ ब्राउज़र उनका समर्थन नहीं कर सकते हैं। अधिक जानने के लिए, जावास्क्रिप्ट घातांक ब्राउज़र समर्थन पर जाएँ।

जावास्क्रिप्ट तुलना ऑपरेटर

तुलना ऑपरेटरों की तुलना दो मूल्यों और एक बूलियन मान लौटने के लिए, या तो trueया false। उदाहरण के लिए,

 const a = 3, b = 2; console.log(a> b); // true 

यहां, तुलना ऑपरेटर >का उपयोग यह तुलना करने के लिए किया जाता है कि क्या बी से अधिक है।

ऑपरेटर विवरण उदाहरण
== Equal to: returns true if the operands are equal x == y
!= Not equal to: returns true if the operands are not equal x != y
=== Strict equal to: true if the operands are equal and of the same type x === y
!== Strict not equal to: true if the operands are equal but of different type or not equal at all x !== y
> Greater than: true if left operand is greater than the right operand x> y
>= Greater than or equal to: true if left operand is greater than or equal to the right operand x>= y
< Less than: true if the left operand is less than the right operand x < y
<= Less than or equal to: true if the left operand is less than or equal to the right operand x <= y

Example 2: Comparison operators in JavaScript

 // equal operator console.log(2 == 2); // true console.log(2 == '2'); // true // not equal operator console.log(3 != 2); // true console.log('hello' != 'Hello'); // true // strict equal operator console.log(2 === 2); // true console.log(2 === '2'); // false // strict not equal operator console.log(2 !== '2'); // true console.log(2 !== '2'); // false

Output

 true true true true true false false true

Comparison operators are used in decision making and loops. You will learn about the use of comparison operators in detail in later tutorials.

JavaScript Logical Operators

Logical operators perform logical operations and return a boolean value, either true or false. For example,

 const x = 5, y = 3; (x < 6) && (y < 5); // true

Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the result is true.

Operator Description Example
&& Logical AND: true if both the operands are true, else returns false x && y
|| Logical OR: true if either of the operands is true; returns false if both are false x || y
! Logical NOT: true if the operand is false and vice-versa. !x

Example 3: Logical Operators in JavaScript

 // logical AND console.log(true && true); // true console.log(true && false); // false // logical OR console.log(true || false); // true // logical NOT console.log(!true); // false

Output

 true false true false

Logical operators are used in decision making and loops. You will learn about the use of logical operators in detail in later tutorials.

JavaScript Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Sign-propagating right shift
>>> Zero-fill right shift

Bitwise operators are rarely used in everyday programming. If you are interested, visit JavaScript Bitwise Operators to learn more.

JavaScript String Operators

In JavaScript, you can also use the + operator to concatenate (join) two or more strings.

Example 4: String operators in JavaScript

 // concatenation operator console.log('hello' + 'world'); let a = 'JavaScript'; a += ' tutorial'; // a = a + ' tutorial'; console.log(a);

Output

 helloworld JavaScript tutorial 

नोट: जब +तार के साथ प्रयोग किया जाता है, तो यह संघनन करता है। हालांकि, जब +संख्याओं के साथ प्रयोग किया जाता है, तो यह अतिरिक्त प्रदर्शन करता है।

अन्य जावास्क्रिप्ट संचालक

यहां जावास्क्रिप्ट में उपलब्ध अन्य ऑपरेटरों की सूची दी गई है। आप बाद के ट्यूटोरियल में इन ऑपरेटरों के बारे में जानेंगे।

ऑपरेटर विवरण उदाहरण
, कई ऑपरेंड का मूल्यांकन करता है और अंतिम ऑपरेंड का मान लौटाता है। let a = (1, 3 , 4); // 4
?: शर्त के आधार पर रिटर्न वैल्यू (5> 3) ? 'success' : 'error'; // "success"
delete किसी ऑब्जेक्ट की संपत्ति, या किसी सरणी का एक तत्व हटाता है delete x
typeof डेटा प्रकार को इंगित करने वाली एक स्ट्रिंग लौटाता है typeof 3; // "number"
void अभिव्यक्ति के वापसी मूल्य को त्यागता है void(x)
in रिटर्न trueअगर निर्दिष्ट संपत्ति वस्तु में है prop in object
instanceof trueयदि निर्दिष्ट ऑब्जेक्ट निर्दिष्ट ऑब्जेक्ट प्रकार का है तो रिटर्न object instanceof object_type

दिलचस्प लेख...