Hone logo
Hone
Problems

Detecting Null Values in JavaScript

JavaScript's null value represents the intentional absence of any object value. Accurately identifying null is crucial for robust error handling and preventing unexpected behavior in your code. This challenge will test your ability to reliably check if a given value is null in JavaScript, considering various potential input types.

Problem Description

You are tasked with creating a JavaScript function called isNull that takes a single argument, value, and returns true if the value is strictly equal to null. The function should return false otherwise. Strict equality (===) is essential to avoid type coercion issues.

Key Requirements:

  • The function must be named isNull.
  • It must accept a single argument named value.
  • It must return a boolean value (true or false).
  • The function must use strict equality (===) to compare the input value with null.

Expected Behavior:

The function should correctly identify null values regardless of the data type of the input. It should return true only when the input is strictly equal to null.

Edge Cases to Consider:

  • undefined: undefined is not null. The function should return false when given undefined.
  • 0: The number zero is not null. The function should return false.
  • "": An empty string is not null. The function should return false.
  • false: The boolean value false is not null. The function should return false.
  • NaN: NaN (Not a Number) is not null. The function should return false.
  • Objects: Objects are not null. The function should return false.
  • Arrays: Arrays are not null. The function should return false.

Examples

Example 1:

Input: null
Output: true
Explanation: The input is strictly equal to null.

Example 2:

Input: undefined
Output: false
Explanation: undefined is not null, even though it represents an absence of value.

Example 3:

Input: 0
Output: false
Explanation: The number 0 is not null.

Example 4:

Input: "hello"
Output: false
Explanation: A string is not null.

Example 5:

Input: { name: "John" }
Output: false
Explanation: An object is not null.

Constraints

  • The input value can be of any JavaScript data type.
  • The function must be implemented using JavaScript.
  • The function must use strict equality (===).
  • The function should be concise and efficient.

Notes

Consider the differences between null and undefined in JavaScript. Using loose equality (==) can lead to unexpected results due to type coercion. Focus on using strict equality (===) to ensure accurate null detection. The goal is to create a function that is reliable and predictable in all scenarios.

Loading editor...
javascript