In this blog post, you will learn about variables in JavaScript, how to declare them, and the difference between var, let, and const, and we will speak about scopes and the visibility of the variables. So, let’s start.
What is a variable and how to declare them?
A variable is a label that references a value like a number or string. Before using a variable, you need to declare it.
To declare a variable, you use the var keyword followed by the variable name as follows:
var age;
A variable name can be any valid identifier. By default, the name variable has a special value undefined if you have not assigned a value to it.
Variable names follow these rules:
• Variable names are case-sensitive. This means that the message and Message are different variables.
• Variable names can only contain letters, numbers, underscores, or dollar signs and cannot contain spaces. Also, variable names must begin with a letter, an underscore (_) or a dollar sign ($).
• Variable names cannot use already used words.
JavaScript is a dynamically typed language. This means that you don’t need to specify the variable’s type in the declaration.
You can also use the let keyword to declare a variable:
let name;
And you can also use the const keyword to declare a variable:
const surname;
Later, in this blog post, we will speak about the differences between var, let, and const keywords.
Once you have declared a variable, you can give a value to it. To do so , you specify the variable name, followed by an equals sign (=) and a value.
For example, The following declares the word variable and gives it this string value "Hello":
var word;
word = “Hello”;
You can give a value to the variable at the same time you declare it:
var word = “Hello”;
Once you initialize a variable, you can change its value by assigning a different value. For example:
var word = “Hello”;
word = “bye”;
console.log(word); //bye
Undefined vs. undeclared variables
It’s important to distinguish between undefined and undeclared variables.
An undefined variable is a variable that has been declared but has not been initialized with a value. For example:
var word;
console.log(word); //undefined
In this example, the word variable is declared but not initialized. Therefore, the word variable is undefined.
In contrast, an undeclared variable is a variable that has not been declared. For example:
console.log(names); //This will give an error
Constants
A constant holds a value that doesn’t change. To declare a constant, you use the const keyword. When defining a constant, you need to initialize it with a value. For example:
const a = 5;
Once defining a constant, you cannot change its value. The following example attempts to change the value of the a constant to 4 and causes an error:
a = 4;//this will give an error
The difference between var and let.
As we know programming languages are made of lots of different operators and tools, such as loops, conditions, functions, and so on. All these tools become complete with the help of blocks. By saying block of code I mean the place where our code is situated. Usually, the start of the block is denoted with { this symbol, and the end of the block with this symbol }.
For example:
{
var x = 10;
}
console.log(x)//10
In this case, the variable x is declared inside the block, but then we use console.log(x) outside the block it will still work. However, if we used let instead of var it would not work:
{
let x = 10;
}
console.log(x);//error
This work by this way, because there are two types of variables: global and local. When we declare variable with var it declares global variable, which we can use even outside the block where it was decalered. And when we use let to declare the variable it declares local variable which can be only used inside the block where it was declared.
Top comments (0)