DEV Community

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

Posted on

1 1

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

Use:

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

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

console.log(largest);

Alt Text

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay