Technically, passing to null will call it with two arguments, so your example doesn't exactly represent this. If you need an answer that is safe for arrow functions (they don't have an object arguments), you can simply check if the second argument is a number. It will catch a lot.
function divide(num1, num2) {
if (typeof num2 !== 'number') return null;
return num1 / num2;
}
If you want to handle the edge when the first argument passed was undefined, you can check both arguments.
function divide(num1, num2) {
if (typeof num1 !== 'number' || typeof num2 !== 'number') return null;
return num1 / num2;
}
If you want a fantasy, it could be a single line arrow:
const divide = (num1, num2) => typeof num2 !== 'number' ? null : num1 / num 2;
source
share