Operators (Arithmetic, Assignment, Comparison, Logical and Conditional)

JavaScript operators families में आते हैं: arithmetic (+ - * / %) compute करता है, assignment (= += ) store करता है, comparison (=== > <) true/false सवाल पूछता है, logical (&& || !) उन्हें combine करता है, और conditional ternary (cond ? a : b) दो values के बीच चुनता है, === safe equality है जो type भी check करती है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

वह fee जो '2001' निकली

FestConnect को 200 की fee में 1 rupee processing charge जोड़नी है। एक student total = fee + charge लिखता है जहाँ fee एक form से text "200" के रूप में आया। Result "2001" print होता है, 201 नहीं।

Operators JavaScript की verbs हैं: वे add करते हैं, compare करते हैं, और decide करते हैं। पर + क्या करता है यह उन types पर depend करता है जिनके बीच यह बैठता है। यह lesson operators को पाँच families में sort करता है ताकि आप हर एक को predict कर सकें।

Formula

पाँच families

  • Arithmetic: + - * / % एक value compute करते हैं। (% remainder है; + strings भी glue करता है।)
  • Assignment: = store करता है; += -= *= in place update करते हैं।
  • Comparison: === !== > < >= <= एक true/false सवाल पूछते हैं।
  • Logical: && (and), || (or), ! (not) true/false answers combine करते हैं।
  • Conditional (ternary): condition ? a : b condition true होने पर a चुनता है, वरना b।

Comparison और logical booleans produce करते हैं; arithmetic values produce करता है; assignment उन्हें store करता है।

At a glance

हर family का एक example

FamilyExampleResult
Arithmetic17 % 52 (remainder)
Assignmentseats -= 1seats 1 से घटता है
Comparisonyear >= 2true या false
Logicalpaid && seats > 0सिर्फ़ तभी true जब दोनों हों
Conditionalseats > 0 ? "Open" : "Full"दोनों में से एक

Practical

FestConnect fee और eligibility

const FEE = 200;
let charge = 1;
let total = FEE + charge;        // arithmetic -> 201

let year = 3;
let paid = true;
let eligible = (year >= 2) && paid;   // logical -> true

let seats = 0;
let status = seats > 0 ? "Open" : "Full";  // ternary -> "Full"

console.log(total, eligible, status);

// the trap: '5' is text, 5 is a number
console.log(5 == "5");   // true  (loose: converts first)
console.log(5 === "5");  // false (strict: type differs)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Output का अंदाज़ा लगाइए

हर एक क्या print करता है? (a) "200" + 1 (b) 200 + 1 (c) 10 % 3 (d) (4 > 2) || false। tap करने से पहले चारों हल कीजिए।

Show the answer

(a) "201" एक string के रूप में: + एक string के साथ glue करता है, तो यह '200' में '1' append करता है। (b) 201 एक number के रूप में: दोनों numbers हैं, तो यह add करता है। (c) 1: modulus 10 को 3 से divide करने का remainder है। (d) true: 4 > 2 true है, और true || कुछ भी true है। (a) बनाम (b) का फ़र्क़ बिल्कुल '2001' bug है: text plus number concatenate करता है।

Quiz

आप `if (userInput === 5)` check करते हैं पर userInput एक form से string "5" के रूप में आया, तो check fail होता है। एक classmate कहता है 'बस == इस्तेमाल कीजिए'। बेहतर fix क्या है और क्यों?

  1. Input को एक number में convert कीजिए (Number(userInput)) और === रखिए
  2. हर जगह == पर switch कीजिए, क्योंकि यह type ignore करता है
  3. Value assign करने के लिए === की जगह = इस्तेमाल कीजिए
  4. JavaScript में एक string को एक number से compare नहीं किया जा सकता
Show the answer

Input को एक number में convert कीजिए (Number(userInput)) और === रखिए

असली problem एक string है जहाँ आपको एक number चाहिए था; इसे Number(userInput) से convert कीजिए और safe, predictable comparisons के लिए strict === रखिए (A)। == पर switch करना (B) bug छुपाता है और दूसरों को invite करता है (== के surprising conversion rules हैं)। = इस्तेमाल करना (C) userInput को 5 assign करेगा, compare नहीं, एक classic error। Strings और numbers बिल्कुल compare किए जा सकते हैं (D ग़लत है)। Operator नहीं, data fix कीजिए।

Watch out

Operator traps

1. == बनाम ===: == पहले types convert करता है (5 == "5" true है), === नहीं करता (5 === "5" false है)। Surprises से बचने के लिए === prefer कीजिए।

2. = बनाम ==: एक अकेला = assign करता है; if (x = 5) x को 5 set करता है और लगभग कभी वह नहीं होता जो आपका मतलब था। Conditions में === इस्तेमाल कीजिए।

3. एक string पर + concatenate करता है: + से text और numbers mix करना add करने के बजाय glue करता है। Form values को पहले Number(...) से convert कीजिए।

Theory

ये अगले decisions को feed करते हैं

Comparison और logical operators वे true/false values produce करते हैं जिन पर बिल्कुल अगला lesson if, while, और for को steer करता है। year >= 2 && paid बिल्कुल वैसा test है जिस पर एक if branch करेगा यह तय करने के लिए कि एक FestConnect student register कर सकता है या नहीं। यहाँ booleans master कीजिए और control structures आसान हो जाते हैं।

Summary

Key takeaways

  • पाँच families: arithmetic (+ - * / %), assignment (= +=), comparison (=== > <), logical (&& || !), conditional (a ? b : c)।
  • Comparison और logical operators true/false return करते हैं; arithmetic values return करता है; assignment store करता है।
  • == से === prefer कीजिए: === value और type दोनों check करता है, == पहले types convert करता है (5 == "5" true है)।
  • = assign करता है, == / === compare करते हैं: किसी condition में कभी = इस्तेमाल मत कीजिए।
  • एक string शामिल होने पर + concatenate करता है; add करने से पहले form text को Number() से convert कीजिए।
  • Memory hook: === strict है, == sloppy है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Overview of JavaScript

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Operators (Arithmetic, Assignment, Comparison, Logical and Conditional) · Web Designing-1 (option A) · Gri-Learn