🌐
Hellojavascript
hellojavascript.info › general javascript questions › javascript fundamentals › while & for loops
While & For Loops | JavaScript Frontend Interview Questions
They are used with break and continue statements, specifically for loops. Technical Response: No, Labels do not allow us to jump into an arbitrary place in the code. A call to break/continue is only possible from inside a loop, and the label must be somewhere above the directive. ... Interview Response: In JavaScript, while-loops check a condition before execution, while for-loops have an initialization, condition, and update expression within the loop statement.
🌐
Medium
melihyumak.medium.com › for-loop-in-javascript-javascript-interview-questions-17-es6-8111c9d0c199
For loop in javascript: Javascript Interview Questions #17 (ES6) | by Melih Yumak | Medium
February 16, 2023 - Let’s see what is the syntax for loop in javascript. for (expression; condition; updateExpression) { // for loop body } Expression is used for declaring variables. Condition is the part checks for the loop will continue or stop.
Discussions

Amazon Web Developer Loop Timeout Interview Question
Typical closure problem. When setTimeout() executes after 3s, it's going to use the value of i at that time, because the function closes over the values from the surrounding block. Old-school way to fix it (updated): for (var i = 0; i < arr.length; i++) { (function(arr, i) { setTimeout(function() { console.log('Index: ' + i + ', value: ' + arr[i]); }, 3000); })(arr, i); } ES6 way: for (let i = 0; i < arr.length; i++) { setTimeout(function() { console.log('Index: ' + i + ', value: ' + arr[i]); }, 3000); } There are more ways around it, but those are probably the most idiomatic. More on reddit.com
🌐 r/javascript
28
29
October 8, 2017
What to expect from this junior developer interview?

Rest, sleep, relax and sip on some tea. You will be fine! If you fail u will learn what to do next time. Dont stress and maybe do some light reading but dont kill urself

More on reddit.com
🌐 r/webdev
9
3
March 6, 2021
Junior dev interview questions?
A big one i get asked ALOT is: "What is dependency injection" Edit: english is hard More on reddit.com
🌐 r/webdev
81
32
May 14, 2019
People also ask

Consider the following code snippet: for (var i = 0; i (a) What gets logged to the console when the user clicks on “Button 4” and why? (b) Provide one or more alternate implementations that will work as expected.
(a) No matter what button the user clicks the number 5 will always be logged to the console. This is because, at the point that the onclick method is invoked (for any of the buttons), the for loop has already completed and the variable i already has a value of 5. (Bonus points for the interviewee if they know enough to talk about how execution contexts, variable objects, activation objects, and the internal “scope” property contribute to the closure behavior.) (b) The key to making this work is to capture the value of i at each pass through the for loop by passing it into a newly created function object. Here are four possible ways to accomplish this: for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.appendChild(document.createTextNode('Button ' + i)); btn.addEventListener('click', (function(i) { return function() { console.log(i); }; })(i)); document.body.appendChild(btn); } Alternatively, you could wrap the entire call to btn.addEventListener in the new anonymous function: for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.appendChild(document.createTextNode('Button ' + i)); (function (i) { btn.addEventListener('click', function() { console.log(i); }); })(i); document.body.appendChild(btn); } Or, we could replace the for loop with a call to the array object’s native forEach method: ['a', 'b', 'c', 'd', 'e'].forEach(function (value, i) { var btn = document.createElement('button'); btn.appendChild(document.createTextNode('Button ' + i)); btn.addEventListener('click', function() { console.log(i); }); document.body.appendChild(btn); }); Lastly, the simplest solution, if you’re in an ES6/ES2015 context, is to use let i instead of var i: for (let i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.appendChild(document.createTextNode('Button ' + i)); btn.addEventListener('click', function(){ console.log(i); }); document.body.appendChild(btn); }
🌐
toptal.com
toptal.com › javascript › interview-questions
Top 37 Technical JavaScript Interview Questions & Answers [2025] ...
What is a potential pitfall with using typeof bar === "object" to determine if bar is an object? How can this pitfall be avoided?
Although typeof bar === "object" is a reliable way of checking if bar is an object, the surprising gotcha in JavaScript is that null is also considered an object! Therefore, the following code will, to the surprise of most developers, log true (not false) to the console: var bar = null; console.log(typeof bar === "object"); // logs true! As long as one is aware of this, the problem can easily be avoided by also checking if bar is null: console.log((bar !== null) && (typeof bar === "object")); // logs false To be entirely thorough in our answer, there are two other things worth noting: First, the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions, you could amend the above solution to be: console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function"))); Second, the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, you could amend the above solution to be: console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]")); However, there’s one other alternative that returns false for nulls, arrays, and functions, but true for objects: console.log((bar !== null) && (bar.constructor === Object)); Or, if you’re using jQuery: console.log((bar !== null) && (typeof bar === "object") && (! $.isArray(bar))); ES5 makes the array case quite simple, including its own null check: console.log(Array.isArray(bar));
🌐
toptal.com
toptal.com › javascript › interview-questions
Top 37 Technical JavaScript Interview Questions & Answers [2025] ...
What is the significance, and what are the benefits, of including 'use strict' at the beginning of a JavaScript source file?
The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice. Some of the key benefits of strict mode include: Makes debugging easier. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions, alerting you sooner to problems in your code and directing you mor
🌐
toptal.com
toptal.com › javascript › interview-questions
Top 37 Technical JavaScript Interview Questions & Answers [2025] ...
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-conditional-statements-and-loops-exercises.php
JavaScript conditional statements and loops - Exercises, Practice, Solution - w3resource
July 10, 2025 - Write a JavaScript program that iterates integers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for multiples of five print "Buzz". For numbers multiples of both three and five print "FizzBuzz".
🌐
GitHub
github.com › sudheerj › javascript-interview-questions
GitHub - sudheerj/javascript-interview-questions: List of 1000 JavaScript Interview Questions · GitHub
Click ⭐if you like the project and follow @SudheerJonna for more updates. Coding questions available here. Check DataStructures and Algorithms for DSA related questions and ECMAScript for all ES features.) Practice 280+ JavaScript coding interview questions in-browser.
Author   sudheerj
🌐
Toptal
toptal.com › javascript › interview-questions
Top 37 Technical JavaScript Interview Questions & Answers [2025] | Toptal®
Note: JavaScript is case-sensitive and here we are using NULL instead of null. ... Explain your answer. How could the use of closures help here? ... The code sample shown will not display the values 0, 1, 2, 3, and 4 as might be expected; rather, it will display 5, 5, 5, 5, and 5. The reason for this is that each function executed within the loop will be executed after the entire loop has completed and all will therefore reference the last value stored in i, which was 5.
🌐
Parikshapatr
parikshapatr.com › interviews › frontend-web-development-interview › javascript-interview › javascript-loops-interview-questions-and-answers
JavaScript Loops - Interview Questions and Answers - ParikshaPatr
October 23, 2024 - Loops in JavaScript are used to ... of code at least once, and then repeats as long as a specified condition is true. for...in loop: Iterates over the properties of an object....
Find elsewhere
🌐
Verve AI
vervecopilot.com › hot-blogs › i-loop-js-interview-skill
What Makes For I Loop Js Such A Critical Skill To Master For Interviews
JavaScript for loop breakdown and examples: GeeksforGeeks · Interview question collections that include loop tasks: CodeSignal, Arc.dev
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-for-loop
JavaScript For Loop - GeeksforGeeks
Some of the loops are: Learn and master JavaScript with Practice Questions.
Published   September 27, 2025
🌐
YouTube
youtube.com › shorts › FAS2Gegksps
🔥 JavaScript For Loop Interview Question: What’s the Output? 🚀 - YouTube
In this video, we break down a tricky JavaScript interview question involving an infinite for loop! 🤯🚀 Question:What happens when you run this code?for(;;)...
Published   February 16, 2025
🌐
InterviewBit
interviewbit.com › javascript-interview-questions
60+ Most Important JavaScript Interview Questions and Answers (2026)
Explore 60+ JavaScript interview questions and answers for 2026, covering basics to advanced topics like closures, hoisting, promises, DOM, and ES6.
Published   1 week ago
🌐
Arc
arc.dev › home › 65 javascript interview questions & answers to prepare for (beg to adv)
65 JavaScript Interview Questions & Answers to Prepare For (Beg to Adv)
1 month ago - What are the main differences between a forEach loop and a map loop? How do you compare two objects in JavaScript? How do you remove duplicates in a JavaScript array? The following set of advanced JavaScript interview questions should test the candidate’s advanced knowledge of JavaScript and some of its more advanced concepts and features.
🌐
InterviewPrep
interviewprep.org › for-loop-interview-questions
Top 25 For Loop Interview Questions and Answers - InterviewPrep
April 30, 2025 - Yes, I can write a code that uses an array and an object in a for loop. Here’s an example using JavaScript:
🌐
JavaScript in Plain English
javascript.plainenglish.io › the-best-javascript-question-for-an-interview-f24435471d9c
The Best JavaScript Interview Question I’ve Ever Come Across | by Nitsan Cohen | JavaScript in Plain English
February 25, 2024 - What would we expect to see in the console? i=0, i=1, i=2, i=3, i=4. You probably already guessed that this is not the right answer. The console will print i=5 five times. And now for the fun part — why? First of all, we have to understand how JavaScript handles the timeout function or more ...
🌐
HackerRank
hackerrank.com › challenges › js10-loops › topics
Day 2: Loops Topics | 10 Days of Javascript Tutorial
You can compile your code and test it for errors and accuracy before submitting.5 of 6
🌐
DEV Community
dev.to › coderslang › javascript-interview-question-39-how-does-settimeout-work-inside-the-loop-33lc
JavaScript Interview Question #39: How does setTimeout work inside the loop? - DEV Community
May 11, 2021 - By declaring i with let, i gets binded to the setTimeout so when it is passed, each callback function that gets passed into the task queue has its own i with the value that was passed in during the for-loop iterations thus printing 0 - 4. ... The question here isn't as much about performance, it's about properly understanding the way asynchronous operations work in JS. ... JavaScript is async by design.
🌐
GreatFrontEnd
greatfrontend.com › blog › 50-must-know-javascript-interview-questions-by-ex-interviewers
50 Must-know JavaScript Interview Questions by Ex-interviewers | Blog
July 10, 2025 - 50 essential JavaScript coding interview questions and answers, curated by senior engineers and former interviewers from leading tech companies.
🌐
Reddit
reddit.com › r/javascript › amazon web developer loop timeout interview question
r/javascript on Reddit: Amazon Web Developer Loop Timeout Interview Question
October 8, 2017 -

Intro (feel free to skip) Hello. I am applying to a Web Developer position at Amazon and have made it through the phone screen with a recruiter and a technical phone interview using coderpad (a collaborative coding platform) with an Amazon engineer as well. During the technical interview, I was asked a question that I got wrong and I am still not sure what the solution is. (I was surprised to recently learn that I will be moving onto the onsite interview because I figured messing up on this question, which I perceive is considered easy, would be the end of my opportunity. But I guess my answers to the other questions, which, for anyone interested were about CSS Box Model, closures, hoisting, and DOM manipulation through JS, led to me passing on.) Any help on what the answer is would be much appreciated.

Interview Question

The interviewer asked me, "What is the output of this following code?":

const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
    setTimeout(function() {
        console.log('Index: ' + i + ', value: ' + arr[i]);
    }, 3000);
}

Even though I thought that was a trick question, I didn't have a better answer than

// Index: 0, value: 10
// Index: 1, value: 12
// Index: 2, value: 15
// Index: 3, value: 21

so that is what I put down as my response. The interview told me that that response was wrong and that the the actual output, after 3 seconds would be:

//Index: 4, value: undefined
//Index: 4, value: undefined
//Index: 4, value: undefined
//Index: 4, value: undefined

He then asked me, "How can you manipulate the above code so that it does print out your answer?" Again, I was not sure (and obviously not really thinking judging my this upcoming answer that I gave), and so I just added arr and i as parameters to the timeout function so the for loop now read:

for (var i = 0; i < arr.length; i++) {
    setTimeout(function(arr, i) {
        console.log('Index: ' + i + ', value: ' + arr[i]);
    }, 3000);
}

I ran this in my console and saw that it also did not work. It just logged the following 4 times:

VM1718:4 Uncaught TypeError: Cannot read property 'undefined' of undefined

(Luckily, right as I wrote my answer in coderpad for the interviewer to see, he said that his browser tab crashed and that he had to reopen the tab and join back into the coding session. When he got back into the session with me after 10 seconds, for some reason, he just moved onto the next question. He seemed to have forgotten that he asked me another question about this timeout problem. Maybe his browser tab crashing saved my interview chances...)

My Question To You Anyone know how the for loop should be changed so that it logs each number and index? Also, what topic is this considered/ what should I read up on so I know more about the logic behind problem?

Thanks.

Edit: Grammar

🌐
Keka
keka.com › javascript-coding-interview-questions-and-answers
JavaScript Coding Interview Questions and Answers List | Keka
Interviewers can analyze the candidate’s knowledge of JavaScript algorithms and mathematical concepts. They expect the candidate to translate a mathematical concept into functional code. To check if a given number is prime, loop from 2 to the square root of the number. If any integer evenly divides it, the number is not prime. ... When asking this question, interviewers are looking for the candidate’s ability to handle nested data structures and apply their knowledge of conditional statements, arrays, and loops.