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 the simplest way to find the smallest number in an array using JavaScript, you've come to the right place. In this article, we'll show you how to do it with just a few lines of code.

Here, we will explain how to use the **Math.min()** function and the spread operator to find the smallest number in an array in JavaScript. The **Math.min() **function returns the smallest of zero or more numbers.

The spread operator allows us to expand an array into a list of arguments. By combining these two features, we can easily find the smallest number in an array.

**Syntax**

`Math.min(...array)`

**Code example**

```
const numbers = [5, 9, 8, 2, 6, 4, 7, 3, 13];
const result = Math.min(...numbers);
console.log("Minimum value is: " + result);
```

**Ouput**

`Minimum value is: 2`

The above code example is using the **spread operator** to find the minimum value in an array.

The spread operator allows an array to be expanded into a list of individual values.

In this case, the spread operator is used to find the minimum value in the array.

**If you do not want to use the spread operator, you can use Math.min.apply() to get the lowest number in an array.**

```
const numbers = [5, 9, 6, 7, 2, 4, 8];
const result = Math.min.apply(Math, numbers);
console.log(result);
```

**Output**

`2`

A **for loop** is a programming construct that allows you to repeat a certain set of code multiple times. In this case, we can use a for loop to iterate through an array of numbers and keep track of the smallest number we've seen so far.

By the time the loop has finished executing, we'll have our answer - the smallest number in the array.

**Code example**

```
const numbers = [2, 5, 9, 1, 6, 7, 4, 8];
let result = numbers[0];
for (let i=0; i < numbers.length; i++) {
if (numbers[i] < result) {
result = numbers[i];
}
}
console.log("Smallest number is: " + result);
```

**Output**

`console.log("Smallest number is: " + result);`

The above code finds the smallest number in an array. It does this by comparing each number in the array to the first number in the array (2). If any number in the array is smaller than 2, it replaces 2 with that number. In the end, the variable result contains the smallest number in the array.

If you need to find the smallest number in an array, the **.reduce()** function can be a helpful tool. This function iterates through an array and returns a single value, which can be the smallest number in the array.

To use **Array.reduce()** to find the smallest number in an array, you need to pass in a callback function that defines how the values in the array will be reduced to a single value. In this case, the callback function would need to find the smallest number in the array and return that value.

```
const numbers = [5, 9, 6, 7, 4, 8, 7, 9, 10, 3, 5, 8, 6];
const result = numbers.reduce((a, b) => Math.min(a, b))
console.log("Minimum value is: " + result);
```

**Output**

`Minimum value is: 3`

This code is finding the minimum value in an array of numbers. The array is called **numbers** and it contains integer type values. The code uses the **reduce** method to iterate through the array and find the minimum value.

The reduce method takes two arguments, **a** and **b**. The first argument is the accumulator and the second argument is the current value. The code is finding the minimum value by iterating through the array and comparing the current value to the accumulator.

If the current value is less than the accumulator, the current value becomes the new accumulator. The code then prints the minimum value to the console.

If you need to find the smallest number in an array using **sort()** in javascript, In the below code examples we will show you how. By default, the **sort()** function sorts numbers in ascending order, so the smallest number will be at the beginning of the sorted array.

**Syntax**

`Array.sort()[0]`

**Code example:**

```
const arr = [20, 90, 50, 30, 60, 10, 80];
const result = arr.sort()[0]
console.log("Smallest value is: " + result);
```

**Output**

`Smallest value is: 10`

The above code example is sorting the array from smallest to largest value and then printing the first element in the array which would be the smallest value.

Was this helpful?

## 0 Comments