Back to blog
Blog note

Largest element in an Array

Finding the Largest Number: Why Your Validation Failed Before You Even Started Looking

PublishedFebruary 10, 2026
AuthorRahul Verma
Readers
Counting views...
Largest element in an Array
Article cover
Share or save

Save this post for later or send it to someone working on SEO, frontend, or product UX.


Finding the Largest Number: Why Your Validation Failed Before You Even Started Looking#

The Real Problem Wasn’t Finding Numbers#

I was working on a seemingly simple task:

“Find the largest number in an array.”

But the real lesson had nothing to do with numbers.

It was about something much more fundamental:

The difference between thinking about a tool and actually using it.

That distinction sounds obvious — until you trip over it in real code.

Here’s What Happened#

I wrote what I thought was clean, well-structured code:

snippet22 lines
function findLargest(arr) {
// Step 1: Check if input is an array
if (!Array.isArray) return false;
// Step 2: Check if array is empty
if (arr.length === 0) return null;
// Step 3: Validate values
for (let value of arr) {
if (typeof value !== "number" || !isFinite(value)) return false;
}
// Step 4: Find the largest number
let largest = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
 

Everything looked right.

But the tests failed with this error:

“Cannot read properties of null (reading 'length')”

That error message was the real teacher.

The Breakthrough Questions That Helped Me#

Question 1: What am I actually checking?#

Look carefully at this line:

snippet
if (!Array.isArray) return false;
 

Ask yourself:

  • What is Array.isArray by itself?

  • Am I checking whether my input is an array, or whether this function exists?

That one question changes everything.

Question 2: What happens step-by-step?#

Let’s trace the code like a computer would.

Someone calls:

snippet
findLargest(null)
 

Now walk through it slowly:

snippet
// Line 1
if (!Array.isArray) return false;
 
  • Array.isArray is a built-in function

  • It always exists

  • !Array.isArray is always false

  • So this check does nothing

Next line:

snippet
if (arr.length === 0) return null;
 

But arr is null.

Trying to read null.length immediately throws an error.

The bug wasn’t in the loop.
The bug happened before the logic even began.

Question 3: What’s the real difference here?#

This is the key insight:

  • Array.isArraythe function itself

  • Array.isArray(arr)the function being used

It’s the difference between knowing a tool exists and actually applying it to something.

The Mental Model That Fixed Everything#

Think Like a Carpenter#

Imagine you’re building a bookshelf.

Wrong thinking:

  1. “I need to check if this material is wood”

  2. “I look at my saw”

  3. “The saw exists, so this must be wood”

That makes no sense.

Right thinking:

  1. “I need to check if this material is wood”

  2. “I use my saw on the material”

  3. “It cuts like wood, so it is wood”

In code terms:#

snippet
Array.isArray // looking at the saw
Array.isArray(arr) // using the saw on the material
 

The parentheses are not syntax noise.
They are the action.

The Correct Solution#

Here’s what the code should have been from the start:

snippet21 lines
function findLargest(arr) {
// FIXED: actually USE the tool
if (!Array.isArray(arr)) return false;
 
// Safe now — arr is guaranteed to be an array
if (arr.length === 0) return null;
 
for (let value of arr) {
if (typeof value !== "number" || !isFinite(value)) return false;
}
 
let largest = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
 
return largest;
}
 

Nothing fancy changed.

Just one line started doing what I thought it was already doing.

Why This Matters Beyond This Problem#

Real-world example: an e-commerce dashboard#

Imagine this logic runs behind a “Highest Sale Today” card:

snippet
const salesData = getSalesData(); // could be null, [], or valid data
const highestSale = findLargest(salesData);
 

Without proper validation:

  • null crashes your UI

  • bad data shows wrong numbers

  • users lose trust

With proper validation:

  • errors are handled

  • empty states make sense

  • logic becomes reliable

This is not about arrays anymore.
It’s about defensive thinking.

The Engineering Mindset#

This bug taught me more than any one-liner solution ever could.

Key takeaways:

  1. Distinguish references from execution

    • A function name ≠ a function call

  2. Validate before you operate

    • Never assume input is clean

  3. Trace code like a machine

    • Especially for null, undefined, and edge cases

  4. Small bugs reveal big thinking gaps

    • Fixing them improves how you reason

The Bigger Picture#

The difference between:

snippet
Array.isArray
 

and

snippet
Array.isArray(arr)
 

is not just two parentheses.

It’s the difference between:

  • thinking about checking

  • and actually checking

Finding the largest number was easy.

Making sure you can safely find it — that was the real challenge.

And that’s what engineering really looks like.