JS का typeof
In this page:
typeof value // "string", "number", "boolean", "object", "function", "undefined"
Basic typeof Checks
किसी value या variable के बाद typeof उसके type को बताने वाली एक स्ट्रिंग return करता है, string, number, boolean, undefined, object, function, symbol, या bigint।
उदाहरण: Basic typeof Checks
// 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 के लिए तभी से बना हुआ है।
उदाहरण: The null Gotcha
console.log(typeof null); // "object" - a long-standing bug
Functions के साथ typeof
typeof functions को उनके अपने distinct type, function, से बताता है, आम object category से अलग, भले ही JavaScript में functions technically एक खास तरह के object ही होते हैं।
उदाहरण: typeof With Functions
function greet() {}
console.log(typeof greet); // "function"
Arrays और Objects के साथ typeof
arrays और plain objects दोनों ही typeof के तहत object बताते हैं, क्योंकि JavaScript में arrays technically एक खास तरह के object होते हैं, इसलिए सिर्फ typeof से इनमें फर्क नहीं किया जा सकता।
उदाहरण: typeof With Arrays and Objects
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।
उदाहरण: The instanceof Operator
// 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);
- यह भूल जाना कि typeof null object return करता है, आपकी उम्मीद वाले null के बजाय एक long-standing quirk।
- arrays को plain objects से अलग करने के लिए typeof इस्तेमाल करना, दोनों object report करते हैं, उस अंतर के लिए Array.isArray() चाहिए।
- यह मान लेना कि 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 हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic