DEV Community

loading...
Cover image for Answer: How might I find the largest number contained in a JavaScript array?

Answer: How might I find the largest number contained in a JavaScript array?

Jeffrey Bodin
github.com/JeffreyBodin Reddit u GuthixIsBalance u waldy713 Instagram instagram.com/jeffreybodin713 Twitter twitter.com/jeffreybodin713 YT youtube.com/user/waldy713/about
・1 min read

Use:

var arr = [1, 2, 3, 4];

var largest = arr.reduce(function(x,y) {
    return (x > y) ? x : y;
});

console.log(largest);

Alt Text

Discussion (0)