Write code that validates variable types:
function validateUser(name, age, active) {
if (typeof name !== "string") {
console.log("Name must be a string")
return false
}
if (typeof age !== "number" || Number.isNaN(age)) {
console.log("Age must be a number")
return false
}
if (typeof active !== "boolean") {
console.log("Active must be a boolean")
return false
}
return true
}
Type validation prevents bugs when working with user input or API data.