Variables and Constants in JavaScript ...!

Variables and Constants in JavaScript ...!

We learned about Introduction to JavaScript in the previous article. Let us begin by learning how to define variables and constants in JavaScript.

Why do you want to study programming? You may desire to study for academic purposes or to create certain apps. When it comes to developing apps, you will need to work with information. Consider the following example:

  • E-Commerce Website - Information might include shopping cart, items sold, etc.

  • Video Conference Application - Information might include users, messages, and much more.

So, where do you save all of this information? Memory is the answer.

Variable

A variable is a name given to a memory location where any type of information can be stored. Information stored in variables can be volatile. If required, information can be updated or altered later in the program.

Rules for Declaring a Variable

  • Variable names are case-sensitive. variableName and VariableName are two different variables.

  • Variables names can contain letters ( a-z, A-Z ), numbers ( 0-9 ), dollar sign ( $ ), or underscore ( _ ) and cannot contain spaces.

  • Variable names must begin with letters ( a-z, A-Z ), dollar sign ( $ ), or underscore ( _ ).

  • Reserved words can't be used as variable names.

Important Points

  • JavaScript allows line breaks and white space while declaring a variable.

  • Multiple declarations can be separated using a comma.

Declaring a Variable

To declare a variable in JavaScript, we can use the let or var keyword.

let variableName;
let message;

//declaring multiple varibale in one line but not recommended 
//let variableName1, variableName2, variableName3;

//rewriting it as
let variableName1;
let variableName2;
let variableName3;

//The console.log() method outputs a message to the web console.
console.log(variableName);//undefined
console.log(message);//undefined

It is good practice to use let keywords for declaring a variable. You will get to know the differences between them later.

In JavaScript, one can declare a variable without using the var or let keyword but it is not a good practice.

Undefined vs Undeclared Variable

A variable that has been declared but not initialized with a value is known as an undefined variable.

let newVariable;
console.log(newVariable);//this will output udefined

A variable that hasn't been declared is known as an undeclared variable.

console.log(count);

Since count variable is not declared, accessing it will throw ReferenceError: count is not defined

You may have also noticed that we did not mention any DataType before variable declaration because JavaScript is a "dynamically typed language," which means you do not need to indicate what data type a variable would hold. The type of a variable is decided and may be altered dynamically while the program runs.

Initializing a Variable

After declaring a variable you can initialize it with a value. To initialize a variable, specify the variable name followed by an equals sign (=), followed by the value you want to give it.

message = "Hello World!";
console.log(message); //Hello World!

Declaring and Initializing a Variable at the same time

This is the option you will most often choose while developing an application.

let info = "Hello World!";
console.log(info); //Hello World!

Updating a Variable

After initializing the variable, we can modify its value by assigning it a new value.

info = "Learnig JavaScript Variables";
console.log(info); //Learnig JavaScript Variables

Variable scope in JavaScript

Variable scope refers to the region or part of a program where a variable is accessible.

In JavaScript, there are two types of scopes:

  1. Local Scope

  2. Global Scope

JavaScript Local Scope

Local scope variables are declared and initialized inside the code block or a function body and it has scope inside the code block or a function body. We can't use local scope variables outside of it and doing so results in a reference error.

Recommended to use let keyword while declaring local variables.

function print(){
    let localVariable = "I am local variable";
    console.log(localVariable);
}
print();//I am local variable
/*console.log(localVariable); since message variable is in local scope, 
ReferenceError: message is not defined*/

JavaScript Global Scope

Global scope variables are declared and initialized anywhere inside the script and it can be accessed anywhere inside the script.

Recommended to use var keyword while declaring global variables.

var globalVariable = "I am global variable";
function result(){
    console.log(globalVariable);
}
result();//I am global variable
console.log(globalVariable);//I am global variable

Constant

Constants are similar to variables in that their value will not change. Once a value is assigned to a constant then you can't reassign the value. To declare constants const keyword is used. While declaring, the constant must be initialized otherwise it will throw a "SyntaxError: Missing initializer in const declaration".

const constantName = value;
const PI = 3.14159265359;
console.log(PI);

The best practice is to use const when you can, and use let when you have to.

Conclusion

In this article, we have learned

  • What is a variable?

  • How do declare, initialize, declare, and initialize and update the variable?

  • Undefined and Undeclared variable.

  • Variable scope in JavaScript.

  • Constant.

Thank you for taking the time to read this article 😊. I hope you found it useful and gained some knowledge. I would highly appreciate it if you take a moment to like 👍, comment ✍️, and share 🔁 it.

Happy Learning...! ✌️✌️✌️

If you liked my content, Connect with me on,

Did you find this article valuable?

Support K Subramanyeshwara by becoming a sponsor. Any amount is appreciated!