Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created January 4, 2023 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrainz/a5c3938934e9f0f00a6f33797d4ce08c to your computer and use it in GitHub Desktop.
Save codebrainz/a5c3938934e9f0f00a6f33797d4ce08c to your computer and use it in GitHub Desktop.
Find the smallest number in an array in JS
function min(arr) {
if (!arr) return NaN;
let min = Number.MAX_VALUE;
for (const element of arr) {
if (element < min) {
min = element;
}
}
return min;
}
console.log(min([2, 0, 42, 222, -4096]));
console.log(min([]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment