Variable and its Scoping in JavaScript

preview_player
Показать описание
Introduction
---------------------
Today, we're diving into variables in JavaScript—how to declare them, their scope,and the concept
of hoisting. Understanding these fundamentals will help you write
efficient and bug-free code.

What is a Variable?
--------------------------------
A variable in JavaScript is a named storage for data values. It acts as a placeholder that allows developers to store,
update, and retrieve data dynamically throughout a program. Variables make it possible to write
flexible and interactive code by enabling data manipulation and logic based on user input or program state.

Declaring Variables
---------------------------------
In JavaScript, we can declare variables using three keywords: var, let, and const.Each behaves
differently. Let me explain.

Variable Hoisting
-----------------------------
Before diving into the variable declaration, it's important to note that all variables in JavaScript
are hoisted to the top of their scope. This means the declaration is moved to the top of its scope
during the compilation phase, though the initialization remains in place.

var Keyword
---------------------
Function-scoped: If a variable is declared with var inside a function, it is accessible anywhere within that function.
Globally-scoped: If declared outside a function, the variable is available globally.
Hoisting: Variables declared with var are hoisted to the top of their scope but are initialized with undefined.
Can be re-declared within the same scope.

let keyword
--------------------
Block-scoped: Limited to the nearest enclosing block (e.g., loops or conditions).
Temporal Dead Zone: Accessing it before declaration causes a ReferenceError.
Re-declaration: Not allowed in the same scope.

const keyword
-------------------------
Block-scoped: Similar to let, const is block-scoped.
Immutable binding: Once a variable is declared with const, its value cannot be changed (although if the value is an object, the object's properties can still be modified).
Not hoisted: Like let, const is hoisted but also in a "temporal dead zone."
Cannot be re-declared within the same scope.

Scope in JavaScript
---------------------------------
Scope refers to the region of the program where a variable is accessible.
There are three main types of scope in JavaScript:

Global Scope
----------------------
A variable declared outside of any function or block is in the global scope,
which means it can be accessed from anywhere in the code.

Function Scope
--------------------------
Variables declared inside a function are in the function scope, meaning they are only accessible within
that function.

Block Scope
---------------------
Variables declared with let and const are
block-scoped. This means they are accessible only within the block (a pair of
curly braces {}) where they are defined, such as inside loops or conditionals.

Lexical Scope
------------------------
Functions can access variables from their parent scope, determined by their position in the source code.

thank you all and please subscribe for more concepts(videos).
Рекомендации по теме
visit shbcf.ru