← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 10 of 12

JS ऑपरेटर्स

अलग-अलग कामों के लिए अलग-अलग tools वाले एक toolbox के बारे में सोचें, कीलों के लिए एक hammer, bolts के लिए एक wrench, size जांचने के लिए एक tape measure, हर tool एक खास तरह का काम करता है। JavaScript operators values के साथ काम करने के लिए वही tools हैं, arithmetic operators गणित करते हैं, comparison operators जांचते हैं कि values एक-दूसरे से कैसे संबंधित हैं, और logical operators true और false results को साथ मिलाते हैं। यह chapter मुख्य operator families का एक व्यापक tour है, हर एक को बाद में अपना गहरा chapter मिलेगा, पर उन्हें पहले साथ देखना cookiescursor.com जैसी sites पर असली code पढ़ते समय मदद करता है।
Syntax
javascript
result = operand1 operator operand2;

Arithmetic Operators

Arithmetic operators संख्याओं पर गणितीय गणनाएँ करते हैं, जिनमें जोड़, घटाव, गुणा, भाग, और शेषफल तथा exponentiation जैसे ज़्यादा विशिष्ट operations शामिल हैं।

Note: + operator strings को जोड़ने के लिए भी काम करता है, उसका व्यवहार पूरी तरह शामिल values के types पर निर्भर करता है।
Warning: Dividing by zero in JavaScript does not throw an error, it returns Infinity or -Infinity instead, which can hide bugs if not checked for.

उदाहरण: Arithmetic Operators

javascript
// Print `10 + 3` to the console
// Print `10 + 3` to the console
console.log(10 + 3);
// Print `10 % 3` to the console
// Print `10 % 3` to the console
console.log(10 % 3);
// Print `2 ** 3` to the console
// Print `2 ** 3` to the console
console.log(2 ** 3);

Comparison Operators

Comparison operators दो values की तुलना करते हैं और boolean नतीजा, true या false, देते हैं, जिनमें बराबर, बराबर नहीं, बड़ा और छोटा शामिल हैं, साथ ही सख्त संस्करण जो type भी जाँचते हैं।

Note: लगभग सभी मामलों में == और != की जगह सख्त === और !== को तरजीह दीजिए, वे type conversion से होने वाले चौंकाने वाले bugs से बचाते हैं।
Warning: == considers 5 (a string) and 5 (a number) equal after converting types, while === correctly treats them as different.

उदाहरण: Comparison Operators

javascript
// Print `5 == "5"` to the console
// Print `5 == "5"` to the console
console.log(5 == "5");
// Print `5 === "5"` to the console
// Print `5 === "5"` to the console
console.log(5 === "5");
// Print `5 > 3` to the console
// Print `5 > 3` to the console
console.log(5 > 3);

Logical Operators

Logical operators boolean values को जोड़ते या उलटते हैं, && (और) के लिए दोनों पक्षों का true होना ज़रूरी है, || (या) के लिए कम से कम एक पक्ष का true होना, और ! (नहीं) किसी boolean के value को पूरी तरह पलट देता है।

Note: Logical operators किसी if statement में कई शर्तों को अलग-अलग nest किए बिना जोड़ने के लिए ज़रूरी हैं।
Warning: && and || don't always return strict true or false, they return one of the original operand values, which matters in more advanced usage.

उदाहरण: Logical Operators

javascript
// Print `true && false` to the console
// Print `true && false` to the console
console.log(true && false);
// Print `true || false` to the console
// Print `true || false` to the console
console.log(true || false);
// Print `!true` to the console
// Print `!true` to the console
console.log(!true);

Assignment और Ternary Operators

Assignment operator = किसी variable में value रखता है, और JavaScript एक संक्षिप्त ternary operator भी देती है, condition ? valueIfTrue : valueIfFalse, जो value बनाने वाले सरल if/else के शॉर्टहैंड के रूप में है।

Note: जब आपको दो सरल values में से चुनना हो तब ternary operator अपनाइए, लेकिन ज़्यादा जटिल logic के लिए पूर्ण if/else का ही उपयोग करते रहिए।
Warning: Nesting multiple ternary operators together can quickly become difficult to read, prefer if/else once logic gets more complex.

उदाहरण: Assignment and Ternary Operators

javascript
// Declare the variable `age`, set to `20`
// Declare the variable `age`, set to `20`
let age = 20;
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
let status = age >= 18 ? "adult" : "minor";
// Print `status` to the console
// Print `status` to the console
console.log(status);

typeof और Spread Operators

typeof किसी value का type बताने वाली string लौटाता है, जो यह जाँचने में उपयोगी है कि आप किस तरह के data के साथ काम कर रहे हैं, जबकि spread operator ... किसी array या object के elements को अलग-अलग फैला देता है, जो आमतौर पर collections की नक़ल बनाने या उन्हें जोड़ने के लिए इस्तेमाल होता है।

Note: आगे प्रोसेस करने से पहले यह त्वरित जाँचने के लिए कि कोई value number, string या कुछ अप्रत्याशित है, typeof खास तौर पर काम का है।
Warning: typeof null famously returns object, a long-standing quirk in JavaScript that catches many developers off guard.

उदाहरण: typeof and Spread Operators

javascript
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof null` to the console
// Print `typeof null` to the console
console.log(typeof null);
// Declare the constant `nums`, set to `[1, 2, 3]`
// Declare the constant `nums`, set to `[1, 2, 3]`
const nums = [1, 2, 3];
// Print `[...nums, 4, 5]` to the console
// Print `[...nums, 4, 5]` to the console
console.log([...nums, 4, 5]);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. = (assignment) को == या === (comparison) से confuse करना, एक single equals sign हमेशा assign करता है, कभी compare नहीं करता।
  2. comparisons के लिए === के बजाय == इस्तेमाल करना, जो automatic type conversion की वजह से अनपेक्षित results दे सकता है।
  3. operator precedence भूल जाना, जैसे किसी mixed expression में यह मान लेना कि addition हमेशा multiplication से पहले होता है।
चैप्टर सारांश
  • Arithmetic operators math करते हैं, comparison operators relationships test करते हैं, और logical operators boolean results को combine करते हैं।
  • assignment operator = एक value set करता है, जबकि === बिना conversion के strictly value और type दोनों compare करता है।
  • Ternary operator एक simple if/else statement का एक compact one-line alternative देता है।
ब्राउज़र सपोर्ट

सभी core JavaScript operators, arithmetic, comparison, logical, assignment, ternary, typeof, और spread, हर modern browser में समान रूप से supported हैं।

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.