JS typeof
In this page:
typeof value // "string", "number", "boolean", "object", "function", "undefined"
Basic typeof Checks
typeof followed by a value or variable returns a string naming its type, string, number, boolean, undefined, object, function, symbol, or 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);
The null Gotcha
typeof null returns object, which is widely considered a bug baked into the earliest versions of JavaScript, kept ever since purely for backward compatibility with existing code.
उदाहरण: The null Gotcha
console.log(typeof null); // "object" - a long-standing bug
typeof With Functions
typeof reports functions with their own distinct type, function, separate from the general object category, even though functions are technically a specialized kind of object in JavaScript.
उदाहरण: typeof With Functions
function greet() {}
console.log(typeof greet); // "function"
typeof With Arrays and Objects
Both arrays and plain objects report as object under typeof, since arrays are technically a specialized kind of object in JavaScript, so typeof alone cannot tell them apart.
उदाहरण: typeof With Arrays and Objects
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof { a: 1 }); // "object"
The instanceof Operator
instanceof checks whether an object was created from a specific constructor, offering more precise type checking than typeof for distinguishing between different kinds of objects, like arrays versus dates versus 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);
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