JavaScript: Get the data type
JavaScript Function: Exercise-9 with Solution
Get Type of Argument
Write a JavaScript function that accepts an argument and returns the type.
Note : There are six possible values that typeof returns: object, boolean, function, number, string, and undefined.
Sample Solution-1:
JavaScript Code:
Output:
number string boolean
Flowchart:

Live Demo:
Sample Solution-2:
JavaScript Code:
Output:
string number boolean null undefined array object
Flowchart:

For more Practice: Solve these Related Problems:
- Write a JavaScript function that returns the type of a given argument and correctly distinguishes between null and object.
- Write a JavaScript function that identifies whether an argument is an array, object, or function using both typeof and instanceof.
- Write a JavaScript function that returns a string indicating the type of a variable, including support for custom class instances.
- Write a JavaScript function that checks an argument’s type and throws an error if it does not match one of the expected types.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function that accepts a number as a parameter and check the number is prime or not.
Next: Write a JavaScript function which returns the n rows by n columns identity matrix.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.