10 > 'a'evaluate to a boolean, rather than throwing an error.
- Can the number be negative? Assuming an input will be positive is an easy mistake. If they can be negative, make sure your function will still produce expected output. And if the input should never be negative, see the above on validation.
- Can it be NaN, infinity or any other non-number number? You won’t be surprised that Infinity, Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY are numbers. But you might be surprised that
typeof NaNevaluates to
'number'. Your code should be able to handle any of these counter-intuitive numbers.
- Is it big enough to cause a stack overflow? Self-explanatory. If you’re dealing with values that may cause a stack overflow, use algorithms or libraries to deal with them.
- Will it always be a decimal? Ok, this is mostly for job interviews. (Note: All of these questions are good to ask when coding in job interviews!) But can you assume inputs won’t be in the other two forms of numbers accepted by vanilla JS, hexadecimal and binary? Remember that parseint() takes any radix between 2 and 36 as its second parameter!
- For collections of numbers, will the numbers be unique? If you know numbers will only occur once, algorithms may be easier to write. And if you know numbers should only occur once, you have another case to validate and test for.
- For collections of numbers, will the numbers be sorted? If you know inputs will be sorted, many operations can be made less expensive.