How to declare variable in JavaScript

In this article, Before learns about how to declare a variable in JavaScript. But before, that let’s understand some about what variable is.

A variable in JavaScript is an identifier that provides a storage location used to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.

In JavaScript programs, a script will have to store the bits of information temporarily. It needs to do its work. It can store this data in variables. In other words, A variable is a symbolic name given to a location in the computer memory. Where we store the value, which we use in the program. The value of the variable can change during the execution of the program. It may take different values at different times during the execution of the program.

The name of the variable that we choose must be meaningful to understand, what it represents in the program.

For example:

var Price = 100;

How to declare a variable in JavaScript.

Using let and const(ES6)

Before 2015, there is only one way to declare a JavaScript variable is var. The 2015 version of JavaScript(ES6) allows the users to define a variable that cannot be reassigned by using the const keyword. The let keyword defines a variable with restricted scope. But today we use the var keyword for declaring a variable.

Creating a variable is known as declaring the variable. Before we use a variable, we need to inform that we want to use it. It requires creating the variable and assigning it a name. To declare a variable we use the var keyword.

var price;

  • var is a keyword. The JavaScript interpreter knows that the var keyword we used to create a variable.
  • To use a variable we must assign it a name is sometimes called an identifier. In the above example, the variable is called price.
  • If the name of the variable contains more than one word. So we use camelCase. It means the first word is all lowercase and any other words have their first letter capitalized.
  • For examplevar myName;

How to assign value to the variable

Once we have created a variable, We can tell it what information we want to store. It known as assigning value to the variable.

For Example:

var price= 40;
  • Now, we can use the variable by its name “price”. Here we set the value for the price variable. Where possible, a variable’s name should describe the data type of the variable.
  • The equals sign(=) is an assignment operator. It says that we are going to assign a value to the variable. We also can use it to update the value given to a variable.
  • After declaring a variable, we haven’t assigned the value to the variable. Technically it has the value of undefined.
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p>Create a variable, assign a value to it, and display it:</p>

<p id="demo"></p>

<script>
var myAge = "24";
document.getElementById("demo").innerHTML =myAge;
</script>

</body>
</html>

Output

JavaScript Variables
Create a variable, assign a value to it, and display it:

24

Re-Declaring JavaScript Variable

We also can re-create the variable without changing its value. For example, if we re-declare myAge will still have the same value 24 after execution of these statements.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p>If you re-declare a JavaScript variable, it will not lose its value.</p>

<p id="demo"></p>

<script>
var myAge = "24";
var myAge;
document.getElementById("demo").innerHTML = myAge;
</script>

</body>
</html>

Output

JavaScript Variables
If you re-declare a JavaScript variable, it will not lose its value.

24

Rules for naming variables

Here are some rules we must follow when assigning a variable name:

  • The name of the variable can contain a letter, digits, underscore(_), and dollar sign($).
  • They must begin with a letter, not with a digit.
  • Uppercase and Lowercase are distinct. That means the variable Total is not the same as total or TOTAl.
  • It should not be a keyword.
  • White space does also not allow.
  • Variable can be any length.

Recommended:

What is variable in JavaScript?

How to define constant in java?

What is the Console in JavaScript.

Reference:

w3school.com

javatpoint.com

Leave a Reply

Your email address will not be published. Required fields are marked *