0

I have this simple function as an example and essentially if the parameter are the wrong type, I want it to return an error which can be read in the browser console that will display in the browsers set language. Instead of using console.error which requires me to enter a custom error, how will I go about this so the error message will explain the parameter types are not valid and in the language set by the browser?

function myFunction(a, b) {
    if (typeof(a) !== 'number' || typeof(b) !== 'number') {
        console.error('Parameter error')
        return null;
    }

    return a + b;
}
bhutto54
  • 7
  • 3

1 Answers1

3

Something like this is what you're looking for. Though, I don't know if the error message will be translated without you having to do additional work.

 function myFunction(a, b) {
     if (typeof(a) !== 'number' || typeof(b) !== 'number') {
         throw new TypeError();
     }
    
     return a + b;
  }
23k
  • 1,596
  • 3
  • 23
  • 52