You Don't Know JS Yet: Scope & Closures
In JavaScript, scope and closures are fundamental concepts that govern the accessibility of variables and functions within a program. Understanding these concepts is crucial for writing maintainable and efficient code.
4.5 out of 5
Language | : | English |
File size | : | 2431 KB |
Text-to-Speech | : | Enabled |
Screen Reader | : | Supported |
Enhanced typesetting | : | Enabled |
Print length | : | 281 pages |
Lending | : | Enabled |
Lexical Scope
JavaScript uses lexical scope, which means that the scope of a variable is determined by its physical location in the code. A variable is accessible within the block in which it is defined, as well as any inner blocks (blocks nested within it).
function outer(){const outerVariable = 1; function inner(){const innerVariable = 2; console.log(outerVariable); console.log(innerVariable); }inner(); }outer();
In this example, the outerVariable
is defined in the outer
function and is accessible within both the outer
and inner
functions. However, the innerVariable
is only accessible within the inner
function.
Variable Environments
Each function in JavaScript has its own variable environment, which is a collection of all the variables that are accessible within that function. The variable environment is created when the function is invoked and is destroyed when the function returns.
When a variable is accessed within a function, the JavaScript engine searches for the variable in the following order:
- The current function's variable environment
- The parent function's variable environment (and so on)
- The global variable environment
If the variable is not found in any of these environments, a ReferenceError
is thrown.
Function Closures
A function closure is a function that has access to the variable environments of the functions in which it was created. This allows functions to retain access to variables even after the functions that defined them have returned.
function outer(){const outerVariable = 1; return function inner(){console.log(outerVariable); }; }const innerFunction = outer(); innerFunction();
In this example, the innerFunction
is a closure because it has access to the variable environment of the outer
function, even though the outer
function has returned. This is possible because the innerFunction
was created within the outer
function and therefore has access to its variable environment.
Applications of Closures
Closures have a wide range of applications in JavaScript, including:
- Encapsulation: Closures can be used to create private variables and methods, which helps to improve code organization and reduce the risk of data leaks.
- Event handling: Closures can be used to capture the state of a function at the time it was created, which is useful for event handling.
- Partial application: Closures can be used to create functions that are partially applied, which can simplify code and make it more reusable.
Understanding scope and closures is essential for writing efficient and maintainable JavaScript code. These concepts govern the accessibility of variables and functions within a program and are used in a wide range of applications.
By mastering these concepts, you will be able to write code that is more organized, reusable, and robust.
4.5 out of 5
Language | : | English |
File size | : | 2431 KB |
Text-to-Speech | : | Enabled |
Screen Reader | : | Supported |
Enhanced typesetting | : | Enabled |
Print length | : | 281 pages |
Lending | : | Enabled |
Do you want to contribute by writing guest posts on this blog?
Please contact us and send us a resume of previous articles that you have written.
- Fiction
- Non Fiction
- Romance
- Mystery
- Thriller
- SciFi
- Fantasy
- Horror
- Biography
- Selfhelp
- Business
- History
- Classics
- Poetry
- Childrens
- Young Adult
- Educational
- Cooking
- Travel
- Lifestyle
- Spirituality
- Health
- Fitness
- Technology
- Science
- Arts
- Crafts
- DIY
- Gardening
- Petcare
- Rowan Ellis
- Stephanie Baier
- Kenneth R Miller
- Jeremy Lent
- Heroes Of The Art
- Leonard Mlodinow
- Kristin Cashore
- Nate Summers
- Linda Spatig
- Santari Green
- Erin Eileen Leigh
- Jaimal Yogis
- Rohan Agarwal
- Lelah Sullivan
- Emma Mckinney
- Zoe S
- Christian Beamish
- Shelby Evans
- Laura Bashar
- Albert S Tarendash
- Joe Baird
- Karen J Rooney
- Slavka Bodic
- Mike Settele
- Bella Bathurst
- Steve Schwartz
- Kate Williams
- Les Carter
- Rose Kearney Nunnery
- Troy Treasure
- Bethany Warren
- Malin Gutestam
- Tracy Tresidder
- Richard Dawkins
- Laurence S Moy
- Gerard Taylor
- Chris Mcintyre
- Michael Pollan
- Chuck Lukacs
- Tim Ingold
- Nicholas Griffin
- Mario Cardano
- David Bain
- Justin Roethlingshoefer
- Prof Dr Wa Liebenberg
- Joshua Rodriguez
- Sherry Turkle
- Scott Wilson
- Darcy Kieran
- Joline Godfrey
- Brian Reddington
- Esther Hicks
- Stacey Barrett
- Paul Dolan
- Bertil Gustafsson
- Nathan Doneen
- John J Watkins
- Frank Ayres
- Shelia Craig Whiteman Pt Dpt Clt
- John C Lundell
- Ricki Linksman
- Si Sheppard
- Mikki Morrissette
- Tim Tebow
- Lawrence J Cohen Phd
- Drew Estell
- Lizzie Lane
- Eileen Kennedy Moore
- L T C Rolt
- John H Holland
- Sarah Maclean
- Ben Hoppe
- Stan Byrdy
- Mark Csikszentmihalyi
- Graham Burgess
- Stephen K Hayes
- Stanley Cohen
- Julie Urbanski
- Rachael Denhollander
- Cathy Jackson
- Lori Ashley Taylor
- Peter Greyson
- Greg Schwipps
- Tom Dodd
- James Blake
- Peter L Berger
- Colin Fletcher
- Chris Batha
- Octavia Steen
- Rich Johnson
- Oliver Sacks
- Monica Swanson
- Eliza Hof
- Ben Worthington
- Emily Glickman
- Tiffany Vincent
- Tony Frezza
- Nancy Redd
- Bill Jamison
- Kim John Payne
- Eva Hoffman
- Kelli L Ferguson
- Casey Schreiner
- Valentin Nugmanov
- Thomas W Miller
- Jay Kirk
- Jane Bottomley
- Kiera Cass
- David Robson
- Glenn Bartley
- Robert W Smith
- C C Hunter
- Holly Parker
- Scott Wallace
- Christopher Hill
- Theresa Bane
- Jonathan Knight
- Adam Stevens
- Joe Clement
- James Henry Breasted
- Helen Leigh
- Martijn Konings
- Fran Zimniuch
- Charles E Farhadian
- Sandy Glenn
- Catherine Faherty
- Department Of The Army
- Ellen Schreiber
- Joe Baker
- Matthew Stein
- Craig Childs
- Lindsey Vonn
- Bill W
- Vinny Berry
- Sandra Brown
- Colin Gordon
- Peter Rendell
- Bob Hazard
- T L Branson
- Sheila Lukins
- Budd Coates
- Chris Cage
- Hansa Bhargava
- Gooseberry Patch
- Brian M Conners
- Theodore Gray
- Editors Of Martha Stewart Living
- Educational Testing Service
- Farley Mowat
- Wesley C Salmon
- Roxanne Reid
- Declan Lyons
- Dan Inosanto
- Scott Wetzler
- Ethan Sawyer
- Henri De Lestapis
- Connie Zweig
- Howard Burton
- Emma Walker
- Blythe Lucero
- Deborah Taylor Hough
- Rich Roll
- Stephen Smith
- Maria Kriya
- Barry King
- William J Callaway
- John Biggar
- Patricia B Mcconnell
- Chris Conley
- Deborah Blum
- Lisa Rene Reynolds Phd
- Will Wraxall
- Stephen Jenkinson
- Christy Teglo
- Suzanne Bender
- Cokie Roberts
- Clifford E Trafzer
- John D Barrow
- Phil Jarratt
- Timothy Gowers
- Brian Sparks
- Tomoko Fuse
- Frank Galli
- William Cane
- Roger Eckstine
- Mindy Cockeram
- Jessica Misener
- Shanterra Mcbride
- John Taylor
- Bernadette Noll
- Josh Gates
- Brent Majcher
- T Edward Nickens
- Beth Reekles
- Bernard Lewis
- Beth Baker
- Jo Boaler
- Mary V Solanto
- John R Bruning
- Rui Zhi Dong
- Sally Moran
- Margaret E Murie
- Bernadette Mcdonald
- Lawrence Wright
- Robert Page
- Rick Rinehart
- Joy Hendry
- Nicola Morgan
- Ciara Attwell
- Stephan A Hoeller
- Naomi Steiner
- Clifford Geertz
- Daniel Muijs
- Michael P Nordvall
- Gaby Melian
- Lebawit Lily Girma
- David Quammen
- Art Friedman
- Tony Horwitz
- Huntley Fitzpatrick
- Ahlia Rose
- Ashley Lecker
- John Fry
- Jean Louis Vidal
- Holly Black
- Harriet A Washington
- W David Pierce
- Meghan Mccarthy
- Dan R Lynch
- James Allen
- Khoi Tu
- Peter Isler
- Ben Gillman
- Mike Brown
- Denise Gaskins
- Don L Gates
- Molly Hurford
- Lisa Mckay
- Bob Raynor
- Kindle Edition
- Kalliope Barlis
- Rick Wiebe
- Jonathan Wolf
- Matt Jacobson
- Stuart James Amei
- Tracy Lash Decrosta
- Simon Armitage
- Dudley Wright
- Ellen Galinsky
- Scott A Hatch
- Sarah Elizabeth Richards
- Lene Fogelberg
- Zecharia Sitchin
- Catherine B Walker
- Fr Mike Driscoll
- S Connolly
- Patricia Leavy
- Nsca National Strength Conditioning Association
- T L Christianson
- Scott Imhoff Phd
- Roger Fredericks
- Bertrand Russell
- Daniel Boyarin
- L H Stacey
- Rebecca Collins
- Phil Stamper
- Deborah D Gray
- Rough Guides
- Debbie Marks
- Sharon Bergen
- Melissa De La Cruz
- Christy Mihaly
- Jo Carol Claborn
- Rachel Ann Cullen
- Adam Griffith
- Kris Wilder
- Kent Allen
- Jeff Grubb
- Gerad Kite
- Ben Smith
- Bb Easton
- Mitch Terrusa
- Gary Oberg
- Kevin Johnson
- Hannah Witton
- Michael S Kelly
- Dean R Giles
- Christy Dorrity
- Vasile Berinde
- Kayla Small
- Hillary Sadler
- Joseph Ciarrochi
- Elizabeth Verdick
- Jenson Button
- Leila Kalmbach
- Elaine Stillerman
- Lizzy Bolan
- Ellen Meloy
- David Diaz
- Eva Robild
- Lesley Pyne
- Shelley C Safian
- Edward O Wilson
- Melissa Ford
- Ben O Williams
- David Lawson Phd
- Julie L Hall
- Nancy Mohrbacher
- Molly Mills
- Dan Orr
- Brandon Sanderson
- Chanda Hahn
- Christian Smith
- Kim Vopni
- Liz Robbins
- Gerald G Briggs
- Nadine Slavinski
- Patricia Hachten Wee
- Carol Devine
- Benjamin Southerland
- Makiia Lucier
- David Barrie
- Lou Kasischke
- David Jeremiah
- Lawrence Baldassaro
- Dave Rearwin
- Neil Iton
- Jon M Sweeney
- Jamie C Martin
- Kyle Simpson
- Sona Charaipotra
- Edward Beauclerk Maurice
- Stefanie Reinhold
- Benjamin Smith
- Bell Hooks
- The Mountaineers
- Joel Hooks
- Richard G Lomax
- Paul Bellow
- Carol Walters
- Philip A Moore
- Lucy Ruddle
- Lucy Cooke
- Charlotte Mary Yonge
- Buck Collins
- Stefanie Molin
- Sarah Spencer
- Rainbow Rowell
- Jill H Rathus
- Peggy J Martin
- Maggie Kline
- Peter Gieler
- Maggie Dent
- Will Oxley
- Vivienne Edgecombe
- Izaak Walton
- Kurt Jacobs
- Oleg Senkov
- John Mandel
- Len Mcdougall
- Martin Lee Abbott
- Print Replica Kindle Edition
- Eliza Larson
- Blandine Calais Germain
- David Rynecki
- Rosalie Knecht
- Lauren Wells
- Larry Gabe
- Steven Rinella
- Marian L Tupy
- Deanna Raybourn
- Christina Hitchcock
- Prem Carnot
- Laura Hoggins
- Dorian Gerhold
- Jordan Rivet
- Alan Levinovitz
- Troy Horne
- Seth A Roberts
- Peter Brown Hoffmeister
- Kara Richardson Whitely
- Mark Lutz
- John E Eck
- Bruce Kirkby
- Sarah Miller
- Lsatmax Lsat Prep
- Patti Jean
- Maggie Oakes
- Melina Palmer
- Percy Keese Fitzhugh
- Sanjay Gupta
- John Dickie
- Jeff Ramey
- Susan Wildes
- Meghan Casey
- Haya Leah Molnar
- Rosamund Hodge
- Knowledge Flow
- Carla Killough Mcclafferty
- Nick Price
- Glenn Randall
- Emily Alison
- Jeremy Griffith
- Ben Crawford
- Ken M Harrison
- Eric Provan
- David Estes
- Joe Farinaccio
- Lawrence S Leff
- Ilya Grigorik
- Mike Veny
- Behan Gifford
- Phil Martin
- Heather Davis
- Janice K Ledford
- Tamarack Song
- Kevin Easley
- Phil Davies
- John Henderson
- Brett Cyrgalis
- Thomas Norman Dewolf
- Olivia Longray
- Bernardo Kastrup
- Leia Stone
- Dennis Fisher
- Charlotte Mason
- Benjamin Johnston
- Gwen Bettwy
- George Daniel
- David Sadker
- Jacqueline Marcell
- Christine Desdemaines Hugon
- Bill Hanstock
- Terrence Talley
- Brigid Kemmerer
- Nicola Griffith
- Evelyn Underhill
- William C Oakes
- Portia Iversen
- Catherine White
- Stephen Renwick
- Jack Buck
- Virginia Smith Harvey
- Janisse Ray
- John Branch
- John Huggan
- John Grasso
- Natasha D Lane
Light bulbAdvertise smarter! Our strategic ad space ensures maximum exposure. Reserve your spot today!
- Samuel WardFollow ·7.5k
- Robert BrowningFollow ·9.6k
- Harvey BellFollow ·11.8k
- Salman RushdieFollow ·8.6k
- David Foster WallaceFollow ·6.4k
- Miguel NelsonFollow ·14.2k
- VoltaireFollow ·5.7k
- Victor HugoFollow ·2.3k
Studying for Your Policing Degree: Critical Study Skills...
Pursuing a policing degree...
Heal Your Heart, Build Trust, & Better Connections To...
In this article,...
Two Mothers, Two Sons, and the Quest to Unlock the Hidden...
When their sons were diagnosed with...
Unveiling the Rivkah Remnant: Lou Kasischke's Unwavering...
In the tapestry of Christian history,...
The Mammoth of the World's Greatest Chess Games: An...
Chess, a game of strategy, intellect,...
Random Tables Fantasy RPG: Random Encounter Tables for...
In the vast tapestry of tabletop role-playing...
4.5 out of 5
Language | : | English |
File size | : | 2431 KB |
Text-to-Speech | : | Enabled |
Screen Reader | : | Supported |
Enhanced typesetting | : | Enabled |
Print length | : | 281 pages |
Lending | : | Enabled |