← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 10 of 26

JS का typeof

किसी border पर एक customs officer की कल्पना करें, जो किसी package को जल्दी से inspect करके उस पर एक category label लगाता है, food, electronics, documents, इससे पहले कि वह आगे बढ़े। typeof operator JavaScript values के लिए वही inspector है, यह जो भी आप इसे देते हैं उसे देखता है और उसके type को बताने वाली एक स्ट्रिंग return करता है, जिससे आपका कोड इस आधार पर decisions ले सकता है कि वह असल में किस तरह के data से डील कर रहा है। इस तरह की runtime type checking असली कोड में आम है, जैसे यह validate करना कि cookiescursor.com पर किसी form field को असल में कोई number मिला है, इससे पहले कि उस पर कोई calculation चलाई जाए।
Syntax
javascript
typeof value  // "string", "number", "boolean", "object", "function", "undefined"

Basic typeof Checks

किसी value या variable के बाद typeof उसके type को बताने वाली एक स्ट्रिंग return करता है, string, number, boolean, undefined, object, function, symbol, या bigint।

Note: typeof को parentheses के साथ और बिना दोनों तरह इस्तेमाल किया जा सकता है, typeof value और typeof(value) बिल्कुल एक जैसा व्यवहार करते हैं।
Warning: typeof हमेशा एक स्ट्रिंग return करता है, इसलिए उसके result की तुलना करने के लिए quotes ज़रूरी हैं, जैसे typeof x === string, न कि typeof x === string।

उदाहरण: Basic typeof Checks

javascript
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof 5` to the console
// Print `typeof 5` to the console
console.log(typeof 5);
// Print `typeof true` to the console
// Print `typeof true` to the console
console.log(typeof true);
// Print `typeof undefined` to the console
// Print `typeof undefined` to the console
console.log(typeof undefined);

null Gotcha

typeof null object return करता है, जिसे व्यापक रूप से JavaScript के सबसे शुरुआती versions में पैठा एक bug माना जाता है, जो मौजूदा code के साथ backward compatibility के लिए तभी से बना हुआ है।

Note: खासतौर पर null को भरोसेमंद तरीके से जांचने के लिए, typeof पर निर्भर रहने के बजाय सीधे strict equality से compare करें, value === null।
Warning: हमेशा याद रखें कि typeof null object है, null नहीं, यह पूरी language में सबसे ज़्यादा बताई जाने वाली अजीब बातों में से एक है।

उदाहरण: The null Gotcha

javascript
console.log(typeof null); // "object" - a long-standing bug

Functions के साथ typeof

typeof functions को उनके अपने distinct type, function, से बताता है, आम object category से अलग, भले ही JavaScript में functions technically एक खास तरह के object ही होते हैं।

Note: typeof value === function असल में कॉल करने से पहले यह confirm करने का एक आम, भरोसेमंद तरीका है कि कोई चीज़ callable है या नहीं।
Warning: Arrow functions, regular functions, और function expressions सभी एक जैसे ही function बताते हैं, typeof इन अलग-अलग function styles में फर्क नहीं कर सकता।

उदाहरण: typeof With Functions

javascript
function greet() {}
console.log(typeof greet); // "function"

Arrays और Objects के साथ typeof

arrays और plain objects दोनों ही typeof के तहत object बताते हैं, क्योंकि JavaScript में arrays technically एक खास तरह के object होते हैं, इसलिए सिर्फ typeof से इनमें फर्क नहीं किया जा सकता।

Note: जब भी आपको यह confirm करना हो कि कोई चीज़ सच में एक array है, खासतौर पर Array.isArray(value) इस्तेमाल करें, उस जांच के लिए typeof काफी नहीं है।
Warning: यह मान लेना कि typeof value === object का मतलब 'plain object' है, arrays और null को भी गलत तरीके से match कर देगा, सटीकता के लिए अतिरिक्त checks चाहिए।

उदाहरण: typeof With Arrays and Objects

javascript
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof { a: 1 }); // "object"

instanceof Operator

instanceof जांचता है कि कोई object किसी खास constructor से बनाया गया था या नहीं, जो अलग-अलग तरह के objects के बीच फर्क करने के लिए typeof से ज़्यादा सटीक type checking देता है, जैसे arrays बनाम dates बनाम custom classes।

Note: instanceof का इस्तेमाल तब करें जब typeof की व्यापक object category काफी specific न हो, जैसे यह confirm करना कि कोई value खासतौर पर एक Date object है।
Warning: instanceof अलग-अलग execution contexts में अप्रत्याशित व्यवहार कर सकता है, जैसे अलग-अलग browser iframes, जहाँ हर एक के अपने अलग constructor references होते हैं।

उदाहरण: The instanceof Operator

javascript
// Declare the constant `arr`, set to `[1, 2, 3]`
// Declare the constant `arr`, set to `[1, 2, 3]`
const arr = [1, 2, 3];
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date();
// Print `arr instanceof Array` to the console
// Print `arr instanceof Array` to the console
console.log(arr instanceof Array);
// Print `date instanceof Date` to the console
// Print `date instanceof Date` to the console
console.log(date instanceof Date);
// Print `arr instanceof Date` to the console
// Print `arr instanceof Date` to the console
console.log(arr instanceof Date);
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. यह भूल जाना कि typeof null object return करता है, आपकी उम्मीद वाले null के बजाय एक long-standing quirk।
  2. arrays को plain objects से अलग करने के लिए typeof इस्तेमाल करना, दोनों object report करते हैं, उस अंतर के लिए Array.isArray() चाहिए।
  3. यह मान लेना कि typeof किसी variable के मौजूद होने को check करने और उसकी current value के type को check करने के लिए same तरीके से काम करता है, वे related लेकिन अलग concerns हैं।
चैप्टर सारांश
  • typeof एक string return करता है जो किसी value का type identify करती है, जैसे string, number, या boolean।
  • typeof null मशहूर तौर पर object return करता है compatibility के लिए preserved एक long-standing historical bug की वजह से।
  • किसी function पर typeof function return करता है, और instanceof ज़्यादा specific object type checks के लिए इस्तेमाल होता है, जैसे किसी array को confirm करना।
ब्राउज़र सपोर्ट

typeof और instanceof core JavaScript operators हैं जो हर browser और JavaScript engine में समान रूप से supported हैं।

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.