Javascript Basics

Javascript Arrays

- Sort array using Array.sort()
- Minimum value from Array
- Maximum value from Array
- Sum of array values
- Create array with blank values
- Remove empty, false, undefined from array
- Remove duplicate values from array
- Combine multiple Arrays
- Check if value is an array
- Array Destructuring in Javascript(ES6)
- Remove falsy value from array
- Shuffle an array
- Remove item from Array
- Array.join() function

Javascript Objects

Javascript Conversions

Javascript Date

Javascript Cookies

Javascript Examples

If you're looking for a quick and easy way to find the third smallest number in an array using Javascript, you've come to the right place. In this article, we'll show you how to do just that with a few simple lines of code.

The third smallest number in an array can be found by first sorting the array in ascending order. Then, the third smallest number will be the number at index 2 in the sorted array.

```
const numbers = [5, 9, 6, 3, 7, 5, 1, 2, 4];
numbers.sort(function (a, b) { return a - b });
console.log("Third smallest number: " + numbers[2]);
```

**Output**

`Third smallest number: 3`

This example shows how to sort an array using the sort() method. The sort method takes a callback function that defines how to sort the array.

In this example, the callback function compares two array elements and returns a value that is used to sort the array.

The sorted array is then logged to the console with the third smallest number being printed.

We can also use **Math.min()** function in Javascript to get the third smallest number in an array. We are removing the two smallest numbers from the copy of the array and then getting the 3rd lowest number.

Check the below code example to understand this.

```
const numbers = [5, 9, 6, 3, 7, 8, 4, 12, 11];
const num_copy = numbers.slice();
for (let i = 0; i < 2; i++) {
const min_index = num_copy.indexOf(Math.min(...numbers));
num_copy.splice(min_index, 1);
};
const result = Math.min(...num_copy);
console.log("Original Array is: ", numbers)
console.log("Third smallest number: " + result);
```

**Output**

```
Original Array is: [5,9,6,3,7,8,4,12,11]
Third smallest number: 4
```

The above code finds the third smallest number in the array. It first copies the array using the slice method.

It then loops through the array twice, each time finding the index of the minimum number and splicing it out of the copied array.

Finally, it finds the minimum number of the remaining numbers in the array and prints it out.

Was this helpful?

## 0 Comments