A variable is a named container in the memory, that stores the values. In simple words, we can say that a variable is a container for values in Javascript. The ES6 Variable names are called identifiers.
The rules to keep in mind while naming an identifier.
- An identifier can contain alphabets and numbers but cannot be the keyword names
- It cannot start with a number
- It cannot contain spaces and special characters, except the underscore (_) and the dollar ($) symbol.
Variable initialization: It refers to the process of storing a value in the variable. A variable initialization can be done at any time before its use.
Valid type of Syntax:
var $variable_name1 = value
var variablename1 = value
var nam$e1 = value
var _name$ = value
The below example illustrates the ES6 Variables:
Example:
javascript
<!DOCTYPE html>
<html>
<head>
<script>
var name = "Geeks" // type string(Dynamic Typing)
console.log("The variable is : " + name);
console.log("<br>Variable Type : " + typeof(name))
</script>
</head>
</html>
Output:
The variable is : Geeks
Variable Type : string
Dynamic Typing: JavaScript supports Dynamic Typing similar to Python, Ruby, Perl, etc. It is a feature where you don't have to tell JavaScript what type of value the variable holds. It gets triggered and takes care of the variable automatically even if the variable value type gets changed while execution.
Variable Scope in JavaScript ES6:
- Global Scope: A variable that can be accessed from any part of the JavaScript code.
- Local Scope: A variable that can be accessed within a function where it is declared.
Example: This example illustrates the Global and Local Scope:
javascript
<!DOCTYPE html>
<html>
<head>
<script>
var $variable_name12 = 10
// Global variable can be accessed
// from anywhere
function Geeks() {
// This local variable gets its
// own memory allocation even
// though it contains the same
// as the outer.
var $variable_name12 = 100
console.log("<br>Inside Geeks() = "
+ $variable_name12)
}
console.log("Outside Geeks() = "
+ $variable_name12)
Geeks();
</script>
</head>
</html>
Output:
Outside Geeks() = 10
Inside Geeks() = 100
The let and const:
- const: The const declaration creates a read-only reference to a value.
Example:
const pi = 3.14
pi = 4.15 // will result in an error!!
In the above, later on, in the code if we try to change the pi value it simply throws an error. Mainly used for mathematical constants. Constants variables are immutable.
- let: This keyword allows the script to restrict access to the variable to the nearest enclosing block. Any variable declared using the let keyword is assigned the block scope. Block scope is a section where the let variable gets a declaration whether it is a block{}, a function{}, or global (script), that section gets restricted to access.
Example:
let n = 100;
let n = 300;
console.log(n);// Throw an error: Identifier 'n' has already been declared
ES6 and Variable Hoisting: Hoisting allows, the use of the variables before their declaration. The concept of hoisting applies to the variable declaration but not variable initialization.
Example:
javascript
<!DOCTYPE html>
<html>
<head>
<script>
// n gets hoisted to the global scope
n = "Geeks";
function geeks() {
// x gets hoisted to the function scope.
for (var x = 0; x < 5; x++) {
document.write("<br>" + x);
}
console.log("<br>Hoisted variable(in function)"
+ " type x : " + typeof(x));
console.log("<br>Hoisted variable(globally)"
+ " type n : " + typeof(n));
}
// Results undefined
console.log("Hoisted variable(out function)"
+ " type x : " + typeof(x));
console.log("<br>Hoisted variable(globally)"
+ " type n : " + typeof(n));
geeks();
</script>
</head>
</html>
Output:
Hoisted variable(out function) type x : undefined
Hoisted variable(globally) type n : string
0
1
2
3
4
Hoisted variable(in function) type x : number
Hoisted variable(globally) type n : string
Note: It is recommended to always declare variables at the top of their scope, to enable the code to resolve the variable’s scope.
Similar Reads
Perl | Variables
Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, deci
4 min read
Less.js Variables
LESS (Leaner Style Sheets) is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that enhances the working power of CSS. LESS supports cross-browser compatibility. CSS pre-processor is a s
4 min read
PHP Variables
A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script.All variable names start with a dollar sign ($).Variables can store different data types, like integers, strin
5 min read
Variables in Cypress
Cypress is a front-end testing tool. It allows us developers and QA engineers who build web applications using the modern JavaScript framework to set up, write, run, and debug tests. It is getting popular as it enables us to write faster, easier, and more reliable tests. Cypress can test anything th
6 min read
Variables in LISP
Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of varia
3 min read
Variables in Research
In the realm of research, particularly in mathematics and the sciences understanding the concept of the variables is fundamental. The Variables are integral to the formulation of hypotheses the design of the experiments and interpretation of data. They serve as the building blocks for the mathematic
6 min read
Variables in TypeScript
Variables in TypeScript are used to store data values, acting as named memory locations that can hold numbers, strings, booleans, or other types of data.Variables can be declared using let, const, or var depending on the use case.They provide type safety by allowing you to define specific data types
4 min read
Variable in Maths
A variable is like a placeholder or a box that can hold different values. In math, it's often represented by a letter, like x or y. The value of a variable can change depending on the situation. For example, if you have the equation y = 2x + 3, the value of y depends on the value of x. So, if you ch
5 min read
JavaScript Variables
Variables in JavaScript can be declared using var, let, or const. JavaScript is dynamically typed, so variable types are determined at runtime without explicit type definitions.JavaScript var keywordJavaScript let keywordJavaScript const keyword JavaScriptvar a = 10 // Old style let b = 20; // Prfer
5 min read
Equations with Variables
An equation in algebra consist of two algebraic expressions separated by an inequality sign generally equal to. For example, ax + b = c. These equations consist of some numbers, a variable, operator sign and inequality sign. An equation with variable is used to represent a general condition . For ex
7 min read