惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
WordPress大学
WordPress大学
IT之家
IT之家
G
Google Developers Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
V
V2EX
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
V
Visual Studio Blog
有赞技术团队
有赞技术团队
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Closure function in JS
Nanthini Amm · 2026-04-30 · via DEV Community

Nanthini Ammu

JavaScript Nested Function :

  • A function can also contain another function. This is called nested function.
function greet(name) {

        function displayName() {
                console.log("Hi " + name)
        }

        displayName();

}

greet("Varun");

Output :
Hi Varun

Enter fullscreen mode Exit fullscreen mode

Returning a function :

  • In JavaScript, it is allowed to return a function within a function.
function greet(name) {

        function displayName() {
                console.log("Hi " + name)
        }

        return displayName; //returning a function

}

const g1 = greet("Varun");
console.log(g1);
g1();

Output :
displayName() {
                console.log("Hi " + name)
        }
Hi Varun

Enter fullscreen mode Exit fullscreen mode

  • In the above program, the greet() function is returning the displayName function definition .
  • The returned function definition is assigned to the g1 variable. When you print g1 using console.log(g1), you will get the function definition.
  • To call the function stored in the g1 variable, we use g1() with parentheses.

What is a Closure?

  • A closure is a function that remembers the variables from its outer scope even after that outer scope has finished executing.
function outer(){
        let count = 0; // This variable lives in outer scope

        function inner(){
                count++
                console.log(count);
        }
        return inner
}

const counter1 = outer(); // outer() finishes, but count isn't gone!
counter1();
counter1();
counter1();

const counter2 = outer();
counter2();
counter2();
counter2();

Output :
1
2
3
1
2
3

Enter fullscreen mode Exit fullscreen mode

  • outer() runs once.
  • But count is not destroyed.
  • Because inner() remembers it.
  • That memory = closure.
  • Even though outer() has returned, inner still holds a live reference to count. That's a closure.
function bank(name,totalAmt)
{
        return {
                deposit : function(amount){
                        totalAmt = totalAmt+amount;
                        return totalAmt
                },
                withdraw : function(amount){
                        totalAmt = totalAmt-amount;
                        return totalAmt
                },
                checkbal : function(){
                        console.log(`Hi ${name}, Your total amount is ${totalAmt}`);
                }       
        }


}

const varunacc = bank("Varun",5000)
console.log(varunacc.deposit(1000));
console.log(varunacc.withdraw(500));
varunacc.checkbal();

Output :
6000
5500
Hi Varun, Your total amount is 5500

Enter fullscreen mode Exit fullscreen mode