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

JS Comparisons

Think of a bouncer at a club checking IDs: loose equality just glances at the name and lets a nickname pass for the real thing, while strict equality demands the ID match exactly, down to the type of document. JavaScript gives you both a loose comparison that converts types before comparing and a strict comparison that never does, and picking the wrong one is one of the most common sources of confusing bugs in beginner code.
Syntax
javascript
a == b   // loose equality
a === b  // strict equality
a != b
a !== b
a > b
a <= b

Loose Equality (==)

== operator तुलना करने से पहले दोनों operands को एक साझा type में बदलता है, इसलिए 5 == 5 true है भले ही एक तरफ़ string हो और दूसरी तरफ़ number।

यह type coercion छोटी scripts में सुविधाजनक है लेकिन JavaScript developers की पीढ़ियों को अप्रत्याशित true/false नतीजों से चौंका चुका है।

उदाहरण: Loose Equality (==)

javascript
console.log('5' == 5);  // true - type coercion

Strict Equality (===)

=== operator किसी भी operand को बदले बिना value और type दोनों की तुलना करता है, इसलिए 5 === 5 false है क्योंकि string कभी सख्ती से number के बराबर नहीं हो सकती।

अधिकांश style guides डिफ़ॉल्ट रूप से === की सिफ़ारिश करती हैं क्योंकि यह उन चौंकाने वाले coercion bugs से बचाता है जो == ला सकता है।

उदाहरण: Strict Equality (===)

javascript
console.log('5' === 5); // false - no coercion, different types

Relational Operators

Relational operators <, >, <= और >= संख्याओं की परिमाण के आधार पर और strings की Unicode code points के अनुसार character-दर-character तुलना करते हैं, जिसे lexicographic order कहते हैं।

इन operators से अलग types के values की तुलना वही तरह का coercion शुरू करती है जो == इस्तेमाल करता है, इसलिए यह जानना फ़ायदेमंद है कि आप किस चीज़ की तुलना कर रहे हैं।

उदाहरण: Relational Operators

javascript
console.log(5 < 10);
console.log("apple" < "banana"); // lexicographic order

अलग-अलग Types की तुलना

null और undefined आपस में loosely equal हैं पर strictly equal नहीं, क्योंकि भीतर से वे अलग types के अलग values दर्शाते हैं।

NaN JavaScript का इकलौता value है जो कभी अपने बराबर नहीं होता, चाहे आप == इस्तेमाल करें या ===, इसीलिए isNaN() या Number.isNaN() खास तौर पर उसे पहचानने के लिए मौजूद हैं।

उदाहरण: Comparing Different Types

javascript
console.log(null == undefined);  // true
console.log(null === undefined); // false
console.log(NaN === NaN);        // false, always

Object.is() और विशेष मामले

Object.is() लगभग हर चीज़ के लिए === जैसा व्यवहार करता है पर दो अपवाद ठीक करता है: यह NaN को खुद के बराबर सही बताता है, और -0 तथा +0 को अलग values के रूप में सही बताता है, जबकि === उन्हें बराबर मानता है।

यह ज़्यादातर JavaScript engines द्वारा भीतर से इस्तेमाल होता है और रोज़मर्रा के code में शायद ही ज़रूरी होता है, पर जानना ठीक है कि यह मौजूद है।

उदाहरण: Object.is() and Special Cases

javascript
console.log(Object.is(NaN, NaN));   // true, fixes the === quirk
console.log(Object.is(0, -0));      // false, distinguishes +0/-0
console.log(0 === -0);              // true
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. #}
🔒

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.