You are currently viewing Variable vs Constant in Javascript | Step-by-Step Guide

Variable vs Constant in Javascript | Step-by-Step Guide

Difference Between Variable and Constant in Javascript

In this article, I would like to discuss the difference between variable and constant in javascript. Let’s understand why variables and constants.

Why variables and constant

While developing any software application, we will be dealing with different types of data and data values. For example, if we are building a banking application then we need to maintain different types of data like customer name, account number if he or she has a loan, etc. Where customer name is a string type of data, the account number is an integer type of data, is he or she has a loan is a boolean type of data.

Another example, If we are building a game then we need to keep track of a variety of data like player name, player score, is game over, etc. Where a player is a string type of data, player score is an integer or number type of data and game over is a boolean type of data.

To store and process all such types of uses in memory, it is possible to locate and access some parts of a memory. It is possible to grab a chunk of memory and access it in the program or a script by providing a meaningful name to it. To allocate a memory location and provide a meaningful name to it, we use a variable or a constant. So, variables and constants are used to allocate memory locations and provide meaningful names to them. That we can be able to access them in a program by their name and we can store different types of values, and reuse them later in the program hasn’t been required.

Declare and initialize a variable/ constant

  • In order to provide a meaningful name to some memory location and use it, we need to declare a variable or a constant.
    Declaring a variable or a constant: means allocating a memory location for some data. By default, the value of a variable will be undefined.
  • In order to change the value in a variable or constant, we need to initialize or assign a value to it.
    Initializing a variable: means putting a data value in that allocated memory location.
READ ALSO  3 Best Ways to Sort Array of Objects Alphabetically in JavaScript

Variables

The variable is a named memory location, whose value may change during the execution of a program.

Declare and initialize a variable

Syntax:

var nameOfVariable; //declaration of a varible
nameOfVariable = value; //initialization of a variable

To declare a variable, we use the keyword “var” then we give a space then we give a name of a variable at the end we put a semicolon. To initialize a variable, we give the variable name then put it equal to and give the value of a variable.

Example:

var playerscore;
document.write("Player score is=", playerscore);

Result:difference between variable and constant in javascript
In the above code, the first line indicates the declaration of a variable “playerscore“. When this line will get executed what the browser actually does means it goes to the memory allocates one memory location or grabs a chunk of memory that memory location it gives a name “playerscore” and it puts a default value “undefined”. The second line of the code indicates, to print the current value of playerscore that is undefined.

How to change the value of a variable at runtime

var playerscore;
document.write("Player score is=", playerscore, "</br>");
playerscore=0;
document.write("Player score is=", playerscore, "</br>");

Result:difference between variable and constant in javascript

In the above code, the browser executes each of the statements one by one.

  1. First executes the first line, the browser will create a variable and assign a name to it “playerscore“.
  2. The second line indicates print the default playerscore i.e undefined value, so it displays “undefined “.
  3. The third line indicates, that the browser will go to the memory location which has the name player score, it erases the default value and puts the new value in whatever we are giving that is ‘0’.
  4. And the last line indicates, prints or displays again the current value of playerscore variable, so it displays “0”.
READ ALSO  How to Use Splice Method in JavaScript

Remember that, you can change the value in a way at any time. In this code, we understood how do we declare a variable in the first line and how do we initialize a variable with value in the third line of code

Constants

Constant is named memory location, whose value never changes during the execution of a program. I.e. Constants stay constant whereas variables vary.

Declaration and initialize of a constant

Syntax:

const NAME_OF_CONSTANT = value; //declare & initialize a constant

To declare a constant, we use a “const” keyword then we give a name of a constant and we say equal to then we give a value and we put a semicolon. Remember that it is not necessary to initialize a variable while declaring whereas you must ensure to give an initialize value to a constant when you declare it. By default the value of a constant will be uninitialized will lead to an error.

Example:

const students = 12;
document.write("No. of Students = ", students);

In the above code, the first line indicates, that the browser goes to the memory allocates one memory location for the constant name “students” and puts the value whatever we have given “12”. And the second line indicates, display, or print the value of the constant. Remember that, we cannot change the constant value during the execution of a program

Note:

  • By default, the value of a variable will be “undefined“.
  • By default, the value of a constant will be “uninitialized” and will lead to an error.

Conclusion

So,  this is the difference between the variable and constant in Javascript.

Leave a Reply