Math methods
The Math
object has many methods that we can use to do calculations and operations on numbers. We will go over the most important ones here. You can see all the available ones when you use an editor that shows suggestions and options during typing.
Finding the highest and lowest number
There is a built-in method max()
to find the highest number among the arguments. You can see it here:
let highest = Math.max(2, 56, 12, 1, 233, 4);
console.log(highest);
It logs 233
, because that's the highest number. In a similar way, we can find the lowest number:
let lowest = Math.min(2, 56, 12, 1, 233, 4);
console.log(lowest);
This will log 1
, because that is the lowest number. If you try to do this with non-numeric arguments, you will get NaN
as a result:
let highestOfWords = Math.max("hi", 3, "bye");
console.log(highestOfWords);
It is not giving 3
as output, because it is not ignoring the text but concluding that it...