JavaScript is a dynamically typed language, which means the types of variable can be changed during runtime. Many interview questions involve recursion of values that contain values of different types and how to handle each value type (e.g. different code is needed to iterate over an array vs an object). Knowledge of handling the JavaScript types is crucial to solving questions like Deep Clone and Deep Equal.
In this question, we will implement the following utility functions to determine the types of primitive values.
isBoolean(value): Return true if value is a boolean, false otherwise.isNumber(value): Return true if value is a number, false otherwise. Note that NaN is considered a number.isNull(value): Return true if value is null, false otherwise.isString(value): Return true if value is a String, else false.isSymbol(value): Return true if value is a Symbol primitive, else false.isUndefined(value): Return true if value is undefined, else false.The code here is short, but the interview skill is choosing the exact runtime check for each type instead of relying on loose equality or overly broad checks.
These helpers fall into three buckets:
typeof works well for primitive categories like strings, numbers, and symbols.null and undefined need exact equality checks because typeof null is 'object' and null == undefined is true.value === true || value === false, which excludes truthy and falsy non-booleans./*** @param {unknown} value* @returns {boolean}*/export function isBoolean(value) {return value === true || value === false;}/*** @param {unknown} value* @returns {boolean}*/export function isNumber(value) {return typeof value === 'number';}/*** @param {unknown} value* @returns {boolean}*/export function isNull(value) {return value === null;}/*** @param {unknown} value* @returns {boolean}*/export function isString(value) {return typeof value === 'string';}/*** @param {unknown} value* @returns {boolean}*/export function isSymbol(value) {return typeof value === 'symbol';}/*** @param {unknown} value* @returns {boolean}*/export function isUndefined(value) {return value === undefined;}
console.log() statements will appear here.