JavaScript Operators: Easy Guide for Beginners

JavaScript में operators और expressions calculations, comparisons, और logical decisions के लिए core building blocks हैं। 

यह ब्लॉग arithmetic, comparison, logical, और assignment operators को गहराई से कवर करता है, साथ ही operator precedence और grouping को समझाता है। Extra theory, practical exercise (BMI calculator), quiz, और operator precedence diagram के साथ आप JavaScript में और confident हो जाएँगे।

JavaScript में ऑपरेटर्स क्या होते हैं? – What is Operators in JavaScript in Hindi?

Operator’s values पर operations perform करते हैं, और expressions operators और operands का combination हैं जो एक value produce करते हैं। उदाहरण: 5 + 3 एक expression है जो 8 देता है।

Theoretical Foundation

  • Operands: Operators जिन values पर काम करते हैं (जैसे 5, 3).

  • Types of Operators: Unary (एक operand, जैसे !true), Binary (दो operands, जैसे 5 + 3), Ternary (तीन operands, जैसे condition? expr1: expr2).

  • Evaluation: JavaScript expressions को left-to-right evaluate करता है, लेकिन precedence aur associativity rules apply होते हैं।

  • Side Effects: कुछ operators (जैसे = या +=) variables की state बदलते हैं, जिसे side effect कहते हैं।

JavaScript में ऑपरेटर के प्रकार – Types of Operators in JavaScript in Hindi

1. Arithmetic Operators

Arithmetic operator’s mathematical calculations के लिए हैं।

  • + (Addition): दो values जोड़ता है।

    let sum = 10 + 5; // 15
  • – (Subtraction): घटाव।

    let diff = 10 - 5; // 5
  • * (Multiplication): गुणा।

    let product = 10 * 5; // 50
  • / (Division): भाग।

    let quotient = 10 / 5; // 2
  • % (Modulus): Remainder देता है।

    let remainder = 10 % 3; // 1
  • ** (Exponentiation): Power calculate करता है।

    let power = 2 ** 3; // 8

Additional Theory:

  • Floating-Point Precision: JavaScript numbers IEEE 754 standard follow करते हैं, जिससे decimal calculations में precision issues हो सकते हैं (जैसे 0.1 + 0.2 !== 0.3)। Solution: toFixed() या libraries जैसे Big.js।

  • Modulus with Negatives: % operator dividend का sign preserve करता है। उदाहरण: -10 % 3 = -1, क्योंकि -10 = (-3 * 3) – 1।

  • Use Cases: Arithmetic operator’s loops, counters, और calculations में common हैं।

2. Comparison Operators

Comparison operators दो values की तुलना करते हैं और Boolean (true/false) return करते हैं।

  • == (Loose Equality): Values की तुलना, type coercion के साथ।

    console.log(5 == "5"); // true
  • === (Strict Equality): Value और type दोनों चेक करता है।

    console.log(5 === "5"); // false
  • != (Loose Inequality): Values में अंतर, type coercion के साथ।

    console.log(5 != "5"); // false
  • !== (Strict Inequality): Value और type में अंतर।

    console.log(5 !== "5"); // true
  • >, <, >=, <=: Greater than, less than, आदि।

    console.log(10 > 5); // true

3. Logical Operators

Logical operator’s conditions को combine करते हैं।

  • && (AND): दोनों conditions true होने पर true।

    let isAdult = 20 > 18 && true; // true
  • || (OR): कोई एक condition true होने पर true।

    let hasAccess = false || true; // true
  • ! (NOT): Condition को invert करता है।

    let isFalse = !true; // false

4. Assignment Operators

Assignment operator’s variables को values assign करते हैं।

  • = (Assign): Value assign करता है।

    let x = 10;
  • +=, -=, *=, /=, आदि: Operation और assignment combine करते हैं।

    let y = 5; y += 3; // y = y + 3; // 8

5. Operator Precedence और Grouping

  • Precedence: Operators का execution order निर्धारित करता है। उदाहरण: *, / की precedence +, – से ज्यादा है।

  • Grouping: Parentheses () precedence ko override karte hain।

    let result = (5 + 3) * 2; // 16

Visual: Operator Precedence Diagram

Operator-Precedence-Diagram
यह diagram operator precedence को visually दर्शाता है। Parentheses precedence को override करते हैं।

Practical Exercise: BMI Calculator

Task: एक program बनाएँ जो user से weight (kg) aur height (m) le aur BMI calculate kare।

let weight = parseFloat(prompt("Enter weight in kg:"));
let height = parseFloat(prompt("Enter height in meters:"));
let bmi = weight / (height * height);
console.log(Your BMI is: ${bmi.toFixed(2)});

Conclusion

इस ब्लॉग में हमने JavaScript के operators और expressions को गहराई से समझा। अगले मॉड्यूल में हम control flow explore करेंगे। Practice करें और सवाल पूछें!

Also Read:

Leave a Comment