Write a function `checkType(val)` that returns the type of the given value as a string. Use the `typeof` operator. For `null`, return `"null"` (not `"object"`).
Problem Statement
Write a function `checkType(val)` that returns the type of the given value as a string. Use the `typeof` operator. For `null`, return `"null"` (not `"object"`).
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
The typeof operator returns a string like "number", "string", etc.
š” Hint 2:
typeof null returns "object" which is a known JavaScript bug. Handle this edge case.
š” Hint 3:
Check if val === null first before using typeof.
ā
Solution:
```javascript
function checkType(val) {
if (val === null) return "null";
return typeof val;
}
```