JavaScript

coding learning websites codepractice

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")?


JavaScript

online coding class codepractice

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

Go Back Top