Hone logo
Hone
Problems

Determining the Data Type of JavaScript Values

Understanding the data type of a variable is fundamental to writing robust and predictable JavaScript code. This challenge asks you to create a function that accurately identifies the type of a given JavaScript value, handling various data types and edge cases correctly. This skill is crucial for data validation, type checking, and ensuring your code behaves as expected.

Problem Description

You are tasked with creating a JavaScript function called getDataType that accepts a single argument, value, and returns a string representing the data type of that value. The function should accurately identify the type of the input, using the typeof operator and handling special cases like null and objects.

Key Requirements:

  • The function must be named getDataType.
  • It must accept a single argument, value.
  • It must return a string representing the data type.
  • The returned string should be lowercase.
  • Handle the null case correctly (return "null").
  • For objects, return "object". Do not attempt to determine the specific type of the object (e.g., "array", "date"). Simply return "object".

Expected Behavior:

The function should return the following strings for the given inputs:

  • getDataType(123) should return "number"
  • getDataType("hello") should return "string"
  • getDataType(true) should return "boolean"
  • getDataType(null) should return "null"
  • getDataType(undefined) should return "undefined"
  • getDataType({}) should return "object"
  • getDataType([]) should return "object"
  • getDataType(function(){}) should return "function"
  • getDataType(new Date()) should return "object"

Edge Cases to Consider:

  • null is a special case that typeof returns "object" for, so you must handle it separately.
  • Arrays are objects in JavaScript, so return "object" for arrays.
  • Functions are objects in JavaScript, so return "object" for functions.
  • Dates are objects in JavaScript, so return "object" for dates.

Examples

Example 1:

Input: 123
Output: "number"
Explanation: The input is a number, so the function should return "number".

Example 2:

Input: null
Output: "null"
Explanation: The input is null, which requires special handling to return "null".

Example 3:

Input: {}
Output: "object"
Explanation: The input is an empty object.  The function should return "object" and not attempt to determine the specific type of object.

Example 4:

Input: [1, 2, 3]
Output: "object"
Explanation: The input is an array, which is an object in JavaScript.

Constraints

  • The input value can be any valid JavaScript value.
  • The function must return a string.
  • The returned string must be lowercase.
  • The function should be efficient and avoid unnecessary computations.

Notes

  • The typeof operator is your primary tool for determining the data type.
  • Remember to handle the null case explicitly.
  • Focus on returning "object" for all object types (arrays, dates, functions, custom objects). Do not attempt to differentiate between these.
  • Consider using a switch statement or a series of if/else if statements for clarity.
Loading editor...
javascript