gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
how to get smallest numer in javascript using math object
- User
how to get smallest numer in javascript using math object
- Assistant
You can find the smallest number in JavaScript using the built-in
Mathobject, specifically theMath.min()method. TheMath.min()function returns the smallest of zero or more numbers. You can provide the numbers as arguments to the function, or you can use the spread operator (...) to pass an array of numbers.Here are a few examples of how to use
Math.min():Example 1: Finding the smallest number from individual arguments
const smallest = Math.min(5, 10, -3, 0, 23); console.log(smallest); // Output: -3Example 2: Finding the smallest number from an array
If you have an array of numbers and want to find the smallest one, you can use the spread operator like so:
const numbers = [12, 43, 2, 59, -8, 15]; const smallest = Math.min(...numbers); console.log(smallest); // Output: -8