Lexical vs Dynamic Scoping

Comments debate lexical scoping versus dynamic scoping in programming languages such as Python, JavaScript, Lisp, and Emacs Lisp, focusing on closures, variable binding, and scope resolution behaviors.

📉 Falling 0.5x Programming Languages
2,771
Comments
20
Years Active
5
Top Authors
#9790
Topic ID

Activity Over Time

2007
5
2008
50
2009
82
2010
102
2011
161
2012
150
2013
168
2014
147
2015
138
2016
146
2017
178
2018
139
2019
151
2020
166
2021
212
2022
199
2023
193
2024
199
2025
182
2026
3

Keywords

AFAIK GETLOCAL PEP UB GC JS python.org DoAll E.g JavaScript scoping scope lexical dynamic foo scoped loop variable variables closures

Sample Comments

codesushi42 Aug 22, 2019 View on HN

... is this a joke? It's called lexical scoping, and it's supported by more languages than not.

Kiro May 4, 2015 View on HN

What is proper lexical scoping? Does JavaScript get it right?

kqr Apr 26, 2018 View on HN

With no scoping you don't, because there is no inner scope.With dynamic scoping you also don't, because there is no lexical scope.What am I misunderstanding?

eslaught Feb 7, 2019 View on HN

Yes, it's called dynamic scoping, and for a long time it wasn't believed that the other option (what we call lexical scoping today) could even be implemented efficiently.https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynam...

gpderetta Nov 28, 2019 View on HN

you have reinvented dynamic scoping :).

stefano May 29, 2009 View on HN

Isn't this similar to dynamic scope?

throw156754228 Jan 2, 2024 View on HN

I see now, they have mutable variables, within a lexical scope anyway.

cjhanks Oct 14, 2016 View on HN

That scope logic is normal in many interpreted languages. I love it in Python.

gpderetta Dec 12, 2025 View on HN

This is the classic lexical vs dynamic scoping. Dynamic scoping works great until it doesn't.

layer8 Jan 12, 2025 View on HN

This is know as dynamic scope, as opposed to lexical scope: https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexic...