JavaScript Null Check
In JavaScript development, it's common to encounter situations where a variable might not have a defined value, leading to potential errors. Robustly checking for null is a fundamental skill that prevents unexpected behavior and ensures your code functions correctly. This challenge will help you practice identifying and handling null values.
Problem Description
Your task is to write a JavaScript function that takes a single argument and returns true if the argument is strictly null, and false otherwise.
Key Requirements:
- The function must be named
isNull. - The function should accept one argument of any data type.
- The function must return a boolean value:
trueif the argument isnull,falseif it's anything else.
Expected Behavior:
- If the input is the primitive value
null, the function should returntrue. - If the input is
undefined, a string, a number, a boolean, an object, an array, or any other JavaScript value, the function should returnfalse.
Edge Cases to Consider:
undefined: This is a common point of confusion withnull. Your function should differentiate between the two.0,false,''(empty string): These are falsy values but are notnull.
Examples
Example 1:
Input: null
Output: true
Explanation: The input is the primitive value `null`, so the function correctly returns `true`.
Example 2:
Input: undefined
Output: false
Explanation: The input is `undefined`, which is distinct from `null`. The function returns `false`.
Example 3:
Input: "hello"
Output: false
Explanation: The input is a string, not `null`. The function returns `false`.
Example 4:
Input: 0
Output: false
Explanation: The input is the number `0`, which is a falsy value but not `null`. The function returns `false`.
Example 5:
Input: {}
Output: false
Explanation: The input is an empty object, not `null`. The function returns `false`.
Constraints
- The function must be implemented in JavaScript.
- The solution should be efficient and avoid unnecessary operations.
- No external libraries or frameworks are allowed.
Notes
Remember that in JavaScript, null and undefined are different. The strict equality operator (===) is your best friend here, as it checks for both value and type without performing type coercion. Consider how === behaves when comparing null to other values.