Variable Declaration and Initializing in JavaScript

1.    Values
Value is a piece of information that can be a number, string, Null, Boolean, Object, or a Function as listed below:

String: It is the series of characters inside double quotes (" ").
Example: "We are learning JavaScript"

Boolean: It is true or false value.
Example: false

Number: It can be any numeric value (without double or single quotes).
Example: 10.13

Null: It has no value.
Example: null

Object: Properties and methods of an object.


2.    Variables
Variable is a place holder or value holder in our computer's memory for information that can changed. This place in memory has been given a name in JavaScript so that we can easily refer to it. Declaring a variable is very easy. We have to do is precede the variable name with the word var as follows:

var variablename;

Once the variable is declared, then you can initialize it with any value shown above.
Here is an example:

variablename = 145.53;
Like the given above example you can change its value to different:

variablename = 65;

We can also change the type by simply assigning a different type of value. We can make this variable a string in the program by simply assigning it one as follows:

variablename = "This is a string in javascript.";

We can initialize a variable and assign it a value at the same time.
Example :
var variablename = "This is javascript.";

You will be using this method most of the time to declare and initialize variables.

I advice that you already try out some of these variables in a simple code like this one:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <script language="JavaScript">
            var variablename = 547;
            document.write(variablename);
        </script>
    </body>
</html>
Now try to change the value of the variable into whatever you like:

<html>
    <head>
        <title>My first Script</title>
    </head>
    <body>
        <center>
            <h1>This Variable decaration and initializing example in JAVASCRIPT.</h1>
            <script language="JavaScript">

                var myVariable = "This is a string in javascript"; //String Type
                document.write(myVariable);
                document.write("<br>");
               
                myVariable = 1234;  // number Type
                document.write(myVariable);
                document.write("<br>");
               
                myVariable = true;  // Boolean Type
                document.write(myVariable);
                document.write("<br>");
               
                var myVariable = null;  // Null Type
                document.write(myVariable);
                document.write("<br>")
               
                myVariable = new Date;  // object Type
                document.write(myVariable);
                document.write("<br>");
           
            </script>
        </center>
    </body>
</html>

The Symbol (//) in given program is used to make single line comment.


0 Comments:

Post a Comment