JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Typeof


📘 JavaScript typeofChecking Data Types in JavaScript

The typeof operator is used in JavaScript to determine the data type of a variable or value. It is useful for debugging and validation.


🔹 Syntax
typeof operand

You can use typeof with variables, literals, or expressions.

typeof "Hello";      // "string"
typeof 42;           // "number"
typeof true;         // "boolean"

🔍 Example Usage
let name = "Alice";
console.log(typeof name);  // "string"

let age = 25;
console.log(typeof age);   // "number"

🔹 Return Values of typeof
Value or Variable typeof result
"Hello" "string"
42, 3.14 "number"
true, false "boolean"
undefined "undefined"
null "object" (✅ JavaScript bug)
[1, 2, 3] "object"
{a: 1} "object"
function() {} "function"
123n (BigInt) "bigint"
Symbol("id") "symbol"

⚠️ Special Case: null
let data = null;
console.log(typeof data);  // "object" ✅ (this is a well-known JavaScript quirk)

🧪 typeof vs. instanceof
  • typeof returns a string with the type

  • instanceof checks whether an object is an instance of a specific constructor


Practice Questions

Q1. How do you check the data type of the string "Welcome" using the typeof operator?

Q2. How do you find the type of a variable isOnline = true?

Q3. What will typeof 100 return, and how do you use it in a console log statement?

Q4. How do you check the type of an undefined variable let result; using typeof?

Q5. What does typeof null return, and why is it considered a JavaScript bug?

Q6. How do you use typeof to check if a function myFunc is of type "function"?

Q7. How do you determine if an array let colors = ["red", "blue"] is an object using typeof?

Q8. How do you use typeof to check the type of a BigInt value 123456789n?

Q9. How do you store the result of typeof in a variable and print it?

Q10. How do you check the type of a symbol declared as let sym = Symbol("key")?


JS Typeof Quiz

Q1: What does typeof "JavaScript" return?

A. string
B. "JavaScript"
C. "string"
D. text

Q2: What is the output of typeof 10?

A. "10"
B. "integer"
C. "number"
D. number

Q3: What will typeof null return?

A. "null"
B. "undefined"
C. "object"
D. "false"

Q4: Which data type returns "function" when used with typeof?

A. Objects
B. Functions
C. Arrays
D. Booleans

Q5: Which of the following is NOT a correct use of typeof?

A. typeof 42
B. typeof "abc"
C. typeof null
D. typeof()

Q6: What is the type of true in JavaScript?

A. "boolean"
B. "true"
C. "object"
D. "string"

Q7: Which keyword is used to define a variable whose type will be checked using typeof?

A. define
B. var / let / const
C. new
D. function

Q8: What does typeof undefined return?

A. "null"
B. "object"
C. "undefined"
D. "void"

Q9: Which of the following statements is true?

A. typeof null is "null"
B. typeof [] is "array"
C. typeof 123n is "bigint"
D. typeof Symbol is "symbol"

Q10: What will typeof {name: "Alex"} return?

A. "array"
B. "object"
C. "function"
D. "dictionary"

Go Back Top