Basic JavaScript- Exercise-05 – Variable Declarations

Variables are the most important element of any programming language. Variables hold values so that you can write expressions using them.You evaluate the expressions and again use a variable to store the results.

In JavaScript, variables are created using keyword “var”.There is no need to declare the type of the variable before assigning a value because as soon as you assign a value to the variable, the type is automatically decided.In this exercise, you will create an external JavaScript file called main.js and an HTML page called ex05.html and save it to a folder on your windows desktop (exercise-05).To know the difference between external and internal JavaScript files, go through exercise-03 and exercise-02.

JavaScript File

In this section, you will create a new text file, insert the example JavaScript code and save the file as main.js.

Step 1: Create a new folder on your windows desktop called “Exercise-05”.

Step 2: Create a new text file inside the “Exercise-05” folder and save it as main.txt.

Step 3: Open the main.txt file and insert the following JavaScript code.

var carname = "Hundai";
var carnumber = 2340;
var num = 10;
var n = 10;
var i = 0;document.write("This is a car and it's Model is" + " " + carname + "");document.write("The car number is " + " " + carnumber + "");
document.write("\n");var txt1 = "What a very";
var txt2 = "nice day";
var txt3 = txt1 + " " + " " + txt2;document.write(txt3 + "

for (i = 0; i < n; i++)
num = num * 4;

document.write("Number=" + num);

Step 4: Save the file as main.jsĀ in “Exercise-05” folder and close it.

The variable names, carname, txt1, txt2 and txt3 are decided using the keyword “var” and assigned a string value “Hyundai”. It automatically becomes a variable of type String.

Similarly, the variable carnumber, num and i are declared using keyword “var” and assigned an integer value, so they automatically become variables of type integer.

We will discuss more JavaScript data types later.

The variables are used for expression and in the example JavaScript, variables txt3 stores the result of the concatenation of two strings – txt1 and txt2. Variable num is multiplied with 4 repeatedly until the loop exits and the final result is stored in variable num itself.

Create an HTML Page

Assuming that you have done all the steps mentioned in the previous section, in this section, you will create an HTML page called ex05.html and link JavaScript file main.js to the HTML document.
Step 1: Create a new text file called ex05.txt in the folder Exercise-05 on your windows desktop.
Step 2: Open the ex05.txt file and insert the following HTML code.
<head><title>Basic Javascript: Exercise-05:Variable Declarations</title>
<p> Javascript Variable, String Concatenation and Loop</p>

Step 3: Add the following script to link the JavaScript file main.js to the HTML page ex05.html between.

<script type="text/javascript" src="main.js"></script>

Output to Browser

You will see the following output in the browser.

Output - JavaScript Variable Declarations
Output – JavaScript Variable Declarations