Hone logo
Hone
Problems

JavaScript: The Undefined Sentinel

In JavaScript, undefined is a primitive value that represents the absence of a value or an uninitialized variable. Accurately detecting when a value is undefined is crucial for robust error handling, conditional logic, and preventing unexpected behavior in your applications. This challenge will test your ability to reliably identify undefined.

Problem Description

Your task is to create a JavaScript function that takes a single argument and returns true if the argument's value is strictly undefined, and false otherwise.

Key Requirements:

  • The function should be named isUndefined.
  • It must accept one parameter.
  • It should return a boolean value.
  • The function must return true only when the input is strictly undefined.
  • For all other data types and values (including null, 0, "", false, etc.), it must return false.

Expected Behavior:

  • If the input is the undefined primitive, return true.
  • If the input is any other value (e.g., null, 0, "", false, true, an object, an array, a number, a string), return false.

Edge Cases to Consider:

  • The difference between undefined and null.
  • Other falsy values like 0, "", false.

Examples

Example 1:

Input: undefined
Output: true
Explanation: The input is the primitive value undefined.

Example 2:

Input: null
Output: false
Explanation: null is a distinct primitive value in JavaScript, not undefined.

Example 3:

Input: 0
Output: false
Explanation: 0 is a number, not undefined.

Example 4:

Input: ""
Output: false
Explanation: An empty string is a string, not undefined.

Example 5:

Input: {}
Output: false
Explanation: An empty object is an object, not undefined.

Constraints

  • The function should be implemented in plain JavaScript, without the use of any external libraries or frameworks.
  • The solution should be efficient and have a time complexity of O(1).

Notes

Remember that undefined is a global property in JavaScript. You can use the strict equality operator (===) to perform a precise check. Consider how other falsy values might behave if you were to use loose equality (==).

Loading editor...
javascript