JavaScript - How to find the Smallest Odd number in an Array

← PrevNext →

It’s super easy. You can use the filter and Math function in JavaScript to find the smallest Odd number in an array.

Heres' a one-liner JS code.

  let get_the_odd = () => {
    let arr = [18, 7, 11, 19, 3];  
    arr = Math.min(... arr.filter(item => item % 2));       //  one-liner JS code.

Try it

find the smallest odd number in an array using javascript

Now, let me break the code for you. First, it will filter out all the odd numbers in the array. Next, it finds the minimum value or the smallest odd number in the array.

  let arr = [18, 7, 11, 19, 3];
  arr = arr.filter(item => item % 2);       // filter out all odd numbers.
  document.write(Math.min(... arr));        // using spread operator with Math.min() method.

Similarly, you can easily filter out only numbers in an Array using JavaScript.

Here's another filter method example, I am sure you will like it. How to filter only 4 letter words from an array in JavaScript. Its so useful, that I created a Cool application called the Random Word Generator.

That's it. Happy coding .

← PreviousNext →