In this video, you will learn how to Find the Second Largest Number in a Given Array in JavaScript using a for loop. We use two variables — max and secondMax — and update them at every iteration by comparing arr[i] with both values.
A complete step-by-step dry run is included so you can trace exactly when max gets updated, when secondMax gets updated, and what the final output is.
This is one of the most commonly asked JavaScript interview questions for beginners — so watch it till the end!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📌 WHAT YOU WILL LEARN
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔢 How to find the second largest element in a JavaScript array
📋 Why max and secondMax are initialized with -Infinity
🔄 How for loop iterates over array using arr.length
✅ How arr[i] greater than max shifts max to secondMax
🧪 Complete dry run — trace both variables at every step
🖥️ How to print second largest using console.log
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 RELATED VIDEOS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
▶ Find Largest Number in Array – [link]
▶ Sum of Array Elements in JavaScript – [link]
▶ Remove Duplicates from Array – [link]
▶ JavaScript Beginner Series Playlist – [link]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
If this video helped you, please give it a LIKE and SUBSCRIBE to AshCodesX for more JavaScript tutorials every week. Drop your questions in the comments — I read every one!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔑 KEYWORDS:
find second largest number in array javascript, second largest element in array javascript, javascript second max in array, javascript max and secondMax, second largest array javascript for loop, javascript array interview question, javascript find 2nd largest element, javascript dry run array, learn javascript 2026, javascript beginner interview programs
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#JavaScript #SecondLargest #JavaScriptArray #JavaScriptInterview #ArrayPrograms #JSforBeginners #LearnJavaScript #DryRun #JavaScriptTutorial #CodingForBeginners #WebDevelopment #AshCodesX #JavaScriptProblems #InterviewQuestions #JavaScriptForLoop