JavaScript Variables Identified Data Storage
Like in most programming languages, variables are essential building blocks of JavaScript. Fundamentally, variables are named values in your code that can represent various values each time the code executes. They can be thought of as either representative names for other things in your program or as containers that hold data. A variable is data “named storage”. What gives a program state is the ability to use variables to store and retrieve values.
Why Make Use of Variables?
If there were no variables, a piece of code would always do the same thing. Working with variables can greatly increase the power of your code and enable it to do a new action each time. You can store data in variables, retrieve them at a later time, and modify them while the code is executing. You will eventually learn how to make variables accept values, even if you may first hardcode them.
Variable Declaration and Assignment
You must declare a variable before using it. A special keyword must be used when declaring a variable for the first time. Modern JavaScript uses let and const for variable declaration, while previous code used var. A variable-defining term, name, and value make up a variable definition.
Variables get data via the assignment operator (=). For instance: x = 2; firstname = “Maaike”;
While the code runs, variables can be allocated new values. In the previous example, variables could be updated: x = 7; firstname = “Edward”
It is also possible to assign a value concurrently with variable definition and initialisation. A variable will have a unique JavaScript undefined value if you declare it without giving it an initial value.
Variable Naming
Label variables with descriptive names that explain their purpose. Numerals, underscores (_), and dollar signs can be in variable names. Also suggest using abstract words like obj, data, value, item, and elem or mixing similar names like date and data to make code harder to understand, though these are presented in a satirical manner. The variables x and X are two distinct names because JavaScript is case-sensitive.
Data Types: Variable Types
A typeless language, JavaScript. The data type of a JavaScript variable does not need to be stated when declaring it. Note that variable data types can change during program execution.
Different types of values are supported by JavaScript. These comprise sophisticated kinds like objects and arrays, as well as primitive types like null, undefined, boolean values (true or false), strings (text), and numbers (integers and floating-point). Any of these values can be stored in a variable.
Variable Scope
The scope of variables is an important idea. Scope determines program variables’ accessibility. It is easier to distinguish between local and global variables when one is aware of scope.
- Local Variables: These are variables that are only available within a specific function’s braces or within the block where they are defined. Variables defined inside a function using the var keyword or by supplying arguments are normally only available within that function, and are removed when the function finishes. With let and const, variables are normally scoped to the block (such an if statement or loop) where they are declared.
- Global variables: All functions can use them. In previous JavaScript, if you created a variable inside a function without using the var keyword, it may mistakenly become a global variable, accessible and editable elsewhere in the program. Global variables should be used with caution, as it’s easy to set the wrong value to one. Global scope refers to variables declared with var outside of any function. Although top-level variables are not global in ES6 modules, variables defined using const or let at the top level of a script may be in some situations regarded as global.
Lexical scope, which determines a variable’s scope based on its code location, is the primary method used by the majority of programming languages, including JavaScript. A variable’s scope is frequently limited to the block in which it is placed.
JavaScript Keywords: let, var, and const

The major JavaScript variable declaration keywords are let, var, and const. These three components usually make up a variable definition: a name (identifier), a value (although initialisation is unnecessary for let and var), and a variable defining keyword (let, var, or const). The variable’s scope (where it is available) and whether its value can be redistributed are impacted by the keyword selection. Most current JavaScript (ES6 and beyond) uses let and const over var.
var Keyword
Prior to ES6, bindings in JavaScript were originally declared using the var keyword. Var and the variable name are used to declare a variable. For instance, var x;. Several variables can be declared using the same var keyword, with commas separating them. Additionally, declaration and initialisation can be combined.
- Scope: Function scope includes var variables. Functions utilizing var statements can only access brace variables. Exiting var functions remove their variables. Top-level code that uses var outside of a function declares global variables that all functions can access. Var variables are assigned to the global object outside of functions. Global variables should be utilized carefully because assigning the wrong value is easy.
- Hoisting: When a function (or script start for globals) is started, var declarations are processed. Hoisting elevates the declaration. Only declaration is raised, not initialization. The var information helps understand previous scripts, suggesting contemporary scripts don’t use it.
- Re-declaration: Var is more tolerant than let and const in re-declaration.
let Keyword
A more recent keyword for declaring variables, let was added in ES6 (ES2015). In contrast to const, let is used for variables whose values may be reassigned later.
- Scope: Letting declares block scope variables. Curly braces {} indicate blocks. Let-defined variables can only be accessible in the code block where they were defined and any descendant blocks. let scopes prohibit variable declaration. The function scope of var differs.
- Temporal Dead Zone (TDZ) Let and const variables cannot be accessed before definition. The temporal dead zone is the period of time between entering the variable’s scope and carrying out its declaration. ReferenceErrors occur when accessing TDZ variables.
- Re-declaration: Unlike var, it is not permitted to redeclarate a variable with let in the same scope. The issue is clearly identified when a variable is redeclared with const.
- Global Scope: Unlike var, let does not establish a property on the global object when defined in the top scope.
const Keyword
Another keyword that was included in ES6 is const. It is applied to constants, which are bindings that have a single value assigned to them and aren’t meant to be changed. One this uses the quote “Ugliness is constant” to allude to the idea of something that never changes.
Initialisation: As indicated by the definition of a constant, you must initialise a constant when you define it with const.
Reassignment: A const variable’s value cannot be altered or reassigned to a different value once it has been initialised.
Immutability: Notably, const denotes an immutable binding, which means that the variable cannot be reassigned to point to a different value. However, if the value is a changeable type, such as an object or array, it does not confer immutability on the value itself. The elements of an array defined with const or the properties of an object can still be altered.
Scope: Const variables have block scope, just like let variables do. Only the block in which they are defined has access to them.
Temporal Dead Zone (TDZ): The temporal dead zone causes a ReferenceError when a const variable is accessed before it is declared, just like let does.
Re-declaration: It is incorrect to redeclarate a constant using const.
let and var Comparison
lets and vars differ most in scope. Unlike var, let is block-scoped. According to modern preference, this scope distinction makes code more predictable. Var variables are accessible across the function they are in, but let variables are only accessible within that block. Let inhibits access before declaration via the TDZ, preventing unexpected undefined values [inferred by comparison]. In modern JavaScript, let and const are preferred over var.
Choosing Between let and const
According to the definition of constant vs. variable, it is generally advised to use const wherever possible and let only when absolutely required.
- For values you know shouldn’t be changed while the program is running, like configuration settings or mathematical constants, use const. It signifies an unchangeable binding.
- If the value of a variable needs to be changed later in the program, use let [implied by contrast with const].
For instance, let [implied by variable nature] must be used in a conventional for loop with a counter that varies with each iteration.
In conclusion, the creation of dynamic and interactive programs requires variables. Their scope and the keyword used for declaration (let, var, or const) dictate their accessibility inside your code. They offer named storage for data and can hold values of any type. Although you cannot directly access or modify this object in your code, the specification internally defines variables as attributes of a “Lexical Environment” object connected to the context in which the code is executed. There were methods for passing and accessing variables between various HTML pages in particular, earlier circumstances, such as when working with HTML frames.