Error Handling in JavaScript

let btnWait1 = document.getElementById(‘btnWait1’)
let inpTimeout1 = document.getElementById(‘inpTimeout1’)

function wait1(timeout, done) {
if (isNaN(parseInt(timeout))) {
return done(new Error(‘Timeout must be a number’))
}

setTimeout(() => {
done(null)
}, timeout)
}

btnWait1.onclick = function () {
wait1(inpTimeout1.value, (err) => {
if (err) console.error(err)
else console.log(‘wait over’)
})
}

Bhaiya,
My Doubt is, In the onclick function , when we pass “err” as a parameter. then in the "if statement’ how does the interpreter automatically detects if err is true or false because we are not comparing it with anything. Then how does the interpreter will know if its an error or not

when we write a variable name inside an if, eg if(err), then it is equivalent to if(err !== undefined).
So if we call done(), then err !== undefined will be false because we didn’t pass anything, so err is undefined. But if we call done(new Error("asdf")), then err !== undefined will be true.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.