Products
Features
YouTube Video Summarizer
Summarize YouTube videos
Web & PDF Highlighter
Highlight web pages & PDFs
Chat with PDF
Ask any PDF questions with AI
Ask AI Clone
Chat with your highlights & memories
Audio Transcriber
Transcribe audio files to text
Glasp Reader
Read and highlight articles
Kindle Highlight Export
Export your Kindle highlights
Idea Hatch
Hatch ideas from your highlights
Integrations
Obsidian Plugin
Notion Integration
Pocket Integration
Instapaper Integration
Medium Integration
Readwise Integration
Snipd Integration
Hypothesis Integration
Apps & Extensions
Chrome Extension
Safari Extension
Edge Add-ons
Firefox Add-ons
iOS App
Android App
Discover
Discover
Ideas
Discover new ideas and insights
Articles
Curated articles and insights
Books
Book recommendations by great minds
Posts
Essays and notes from readers
Quotes
Inspiring quotes collection
Videos
Curated videos and summaries
Explore Glasp
Glasp Newsletter
Weekly insights and updates
Glasp Talk
Interview series with great minds
Glasp Blog
Latest news and articles
Glasp Use Cases
Learn how others use Glasp
Build & Support
Glasp API
Access Glasp's API for developers
MCP Connector
Connect Glasp to Claude & ChatGPT
Community
Glasp Reddit Community
Students
Student discount and benefits
FAQs
Frequently Asked Questions
AboutPricing
DashboardLog inSign up

setTimeout + Closures Interview Question 🔥 | Namaste 🙏 JavaScript Ep. 11

542.0K views
•
December 7, 2020
by
Akshay Saini
YouTube video player
setTimeout + Closures Interview Question 🔥 | Namaste 🙏 JavaScript Ep. 11

TL;DR

Closures in JavaScript allow functions to remember and access variables from their lexical environment, even when executed in a different scope.

Transcript

now we will cover very important interview questions related to closures which is asked a lot of times in interviews and you know even very senior developers they pull their hairs right when they see like things are not working as expected and you know that is because we don't understand what happens behind the scenes so now let us see these exampl... Read More

Key Insights

  • 🫥 The set timeout function in JavaScript does not wait for the timer to expire before executing the next line of code.
  • 👻 Closures in JavaScript allow functions to retain access to variables from their lexical environment.
  • 💌 The "let" keyword in a for loop can create a new copy of a variable for each iteration, solving issues with incorrect values in asynchronous operations.

Install to Summarize YouTube Videos and Get Transcripts

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: Why does the set timeout function not wait for the timer to expire in JavaScript?

JavaScript does not wait for anything and continues to execute the next line of code. The set timeout function attaches a timer to the callback function and stores it for later execution.

Q: What is a closure in JavaScript?

A closure is a function that retains access to variables from its lexical environment, even if executed in a different scope. It allows functions to "remember" and access these variables.

Q: How can closures help solve the problem of printing the wrong values in a loop?

By using the "let" keyword in the loop, a new copy of the variable is created for each iteration. This ensures that each set timeout function has its own unique reference to the variable.

Q: Why does using the "var" keyword in the loop result in printing the wrong values?

The "var" keyword does not create a new copy of the variable for each iteration. Instead, all set timeout functions refer to the same variable, which may have been modified by the time they execute.

Key Insights:

  • The set timeout function in JavaScript does not wait for the timer to expire before executing the next line of code.
  • Closures in JavaScript allow functions to retain access to variables from their lexical environment.
  • The "let" keyword in a for loop can create a new copy of a variable for each iteration, solving issues with incorrect values in asynchronous operations.
  • Understanding closures can help programmers avoid common interview challenges and better understand JavaScript's behavior.

Summary & Key Takeaways

  • Closures are frequently asked interview questions and can be difficult to understand without proper knowledge.

  • The set timeout function in JavaScript does not wait for the timer to expire before running the next line of code.

  • Using the "let" keyword in a for loop can create a new copy of a variable for each iteration, solving the issue of printing the wrong values.


Read in Other Languages (beta)

English

Share This Summary 📚

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator

Explore More Summaries from Akshay Saini 📚

Tab Form Component (Myntra, Zepto) - Frontend Interview Question using React thumbnail
Tab Form Component (Myntra, Zepto) - Frontend Interview Question using React
Akshay Saini
[ Live ] React JS Interview 🤯 | Frontend UI Questions 🤒 thumbnail
[ Live ] React JS Interview 🤯 | Frontend UI Questions 🤒
Akshay Saini
Best ReactJS Live course, Namaste React Live 🚀 thumbnail
Best ReactJS Live course, Namaste React Live 🚀
Akshay Saini
Autocomplete Search Bar (Amazon, Ola) - Frontend Interview Question using React thumbnail
Autocomplete Search Bar (Amazon, Ola) - Frontend Interview Question using React
Akshay Saini
Promises | Ep 02  Season 02 - Namaste JavaScript thumbnail
Promises | Ep 02 Season 02 - Namaste JavaScript
Akshay Saini
How JavaScript Code is executed? ❤️& Call Stack | Namaste JavaScript Ep. 2 thumbnail
How JavaScript Code is executed? ❤️& Call Stack | Namaste JavaScript Ep. 2
Akshay Saini

Summarize YouTube Videos and Get Video Transcripts with 1-Click

Download browser extensions on:

Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator

Apps & Extensions

  • Chrome Extension
  • Safari Extension
  • Edge Add-ons
  • Firefox Add-ons
  • iOS App
  • Android App

Key Features

  • YouTube Video Summarizer
  • Web & PDF Summarizer
  • Web & PDF Highlighter
  • Chat with PDF
  • Ask AI Clone
  • Audio Transcriber
  • Glasp Reader
  • Kindle Highlight Export
  • Idea Hatch

Integrations

  • Obsidian Plugin
  • Notion Integration
  • Pocket Integration
  • Instapaper Integration
  • Medium Integration
  • Readwise Integration
  • Snipd Integration
  • Hypothesis Integration

More Features

  • APIs
  • MCP Connector
  • Blog & Post
  • Embed Links
  • Image Highlight
  • Personality Test
  • Quote Shots

Company

  • About us
  • Blog
  • Community
  • FAQs
  • Job Board
  • Newsletter
  • Pricing
Terms

•

Privacy

•

Guidelines

© 2026 Glasp Inc. All rights reserved.