How to use typeof with arguments in JavaScript?

The arguments object contains all parameters passed to a function. You can use typeof to check the data type of individual arguments or the arguments object itself.

Understanding the arguments Object

The arguments object is array-like and accessible within all functions. You can access individual arguments using bracket notation:

arguments[0]  // First argument
arguments[1]  // Second argument

Using typeof with the arguments Object

When you use typeof on the arguments object itself, it returns "object":



   


Type of arguments: object
Arguments object: Arguments(3) ["hello", 42, true]

Checking Types of Individual Arguments

You can use typeof to check the data type of each argument individually:



   


Argument 0 type: string
Argument 0 value: string
Argument 1 type: number
Argument 1 value: 123
Argument 2 type: boolean
Argument 2 value: true
Argument 3 type: object
Argument 3 value: null
Argument 4 type: object
Argument 4 value: {name: "object"}

Practical Example: Type Validation

Here's a practical example that validates argument types in a function:



   


Skipping non-number: hello (type: string)
Skipping non-number: true (type: boolean)
Sum of 3 numbers: 60

Key Points

  • typeof arguments always returns "object"
  • typeof arguments[index] returns the actual type of that argument
  • The arguments object has a length property for iteration
  • Use this technique for dynamic type checking in functions

Conclusion

Using typeof with arguments allows you to validate and handle different data types passed to functions. This is particularly useful for creating flexible functions that accept various parameter types.

Updated on: 2026-03-15T22:11:05+05:30

901 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements