Tips For Navigating Variables and Functions in JavaScript Scope
I landed on Mars of Reading Code... This doesn't make any sense, YET!
The dynamic and interactive components of the web are powered by JavaScript, a language renowned for its adaptability and agility. The idea of scope is essential to its operation; it determines the availability(visibility) to see a lifetime of variables and functions in your code. We'll explore JavaScript scope in detail in this blog, removing layers and offering tips that can help you develop more effective and manageable code.
All of this might sound overwhelming at first or just in general. That's okay! I've been overwhelmed and find myself still wondering why certain variables and functions are necessary in JavaScript. I mean, I did just learn what JavaScript was 3 weeks ago. This blog is an ode to all the women (and men) who just need a little more dissecting of the topic at hand. Let's dive in, together.
Building Blocks Of Scope!
It all starts with the Fundamentals [of Scope]: Fundamentally, JavaScript scope specifies the environment in which variables and functions are defined and used. Composing reliable and error-free code requires a thorough understanding of the scope. In JavaScript, there are three main kinds of scope: Global scope, Local (Function) scope and Block scope.
First Up: Global Scope!
Global Scope: A variable is considered to have a global scope if it is defined outside of any block or function. These variables are strong but also potentially dangerous because you can access them from anywhere in your code. Because global variables are modifiable by any portion of your program, they might result in naming conflicts and unexpected side consequences.
In this example, we defined a variable 'personName
' and a function 'printName' in the global scope. Since the global scope is the broadest scope in JavaScript, any other scope inside the JavaScript application can access the 'personName
' variable and the 'printName
' function.
Main Tip : Remember Global Scope is the default scope for all code running in script mode. Global scope will contain all the information on the front end of the code and be visible in all other scopes.
Next Up: Local (Function) Scope!
Local (Function) Scope: Variables declared within a function or block have local scope, which means they are only available within that context. By enclosing functions, this lowers the possibility of unintentional variable mutation. Local scope in JavaScript refers to the scope established within a function. Functions, objects, and variables specified inside local scope are solely accessible from within the context of that function; they cannot be accessed from outside of it.
The above example shows 'maleName
' is a variable in the global scope, but 'age
' is a variable in the local scope of the 'printName
' function. Due to its placement within the 'printName
' function, the 'printDetails
' function obtains access to both the local scope variable 'age' and the global scope variable 'maleName
'. JavaScript uses the method known as variable hoisting*, 'maleName
' would also be readily accessible if we attempted to use it straight from the 'printDetails
' function. However, if we tried to extract 'age
' from 'printDetails
' function, the result would be an error. This is due to JavaScript only being able to access variables within the current scope of the function. JavaScript, by nature, will not look up the scope chain in order to find the variable presented in the parent scope.
Main Tip : Each function creates a NEW scope. Remember local variables have function Scope because of how they can only be accessed from within the function. Having a good understanding how local scope works is crucial for maintaining a clean and modular code.
*Variable hoisting moves the variable declaration to the top of the scope, causing the code to act as if it were the first statement performed in the function.
LAST but technically first? No sé... But you guessed it :
Block Scope!
Two words: let
and const
! In 2015, before ECMAScript 6 or ES6, the only JavaScript variables presented were Global Scope and Function Scope. ES6 created the two very important JavaScript keywords: let
and const
. With the creation of these two keywords it provided the birth of Block Scope in JavaScript. It goes like this, the variables declared inside a set of a curly brace, { }, block cannot be accessed from outside the block.
In the example, we see that once we declared the 'exampleBlock
' function we nested inside the curly brackets,{}
, the information that is confined within the function to create block scope. The 'blockScopedVar
' and console.log
are the meat and potatoes of the block scope showing that once the code is ran and completed the block scope has run its course.
Main Tip: Block scope is established once a let or const is declared. Anything that is declared within the curly brackets, {}
, cannot be accessed outside the block.
[To make a] short story shorter..
The topic of Scope in JavaScript is a fundamental concept that will continue to shape the behavior and structure of your code. By becoming the master the nuances of Global, Local (Function) scope, and Block scope by understanding the scope chain, and developing more dependable and maintainable JavaScript code by utilizing block-scoping with let
and const
. You'll be a force to be reckoned with! Following these tips you'll be able to embrace scope as a one of the most powerful tools in your coding toolbox, enabling you to dissect the complexities of development with confidence and clarity. Happy Scoping!