Home > DeveloperSection > Articles > JavaScript Variables and Data types

JavaScript Variables and Data types


JavaScript JavaScript 
Ratings:
0 Comment(s)
 2438  View(s)
Rate this:

JavaScript Variables and Data types

Introduction:

This is a very important article as I am going to explain which data types Java Script supports and how to declare variables in Java Script and use them.

JavaScript DataTypes:

Data types are one of the fundamental characteristics of any programming languages. Data Type are the types which represent the type of value which can be represented and manipulated in any of the Programming Language. Like every Programming Language have data types, it is also available in Java Script.

There are three primitive data types in Java Script they are:

1.                   Numbers

2.                   String of Text

3.                   Boolean

In addition to these, Java Script also has null and undefined data types.

Undefined Value: There are cases when value will be provided after any computation, or will be provided later. In that case the value will be undefined.

Numbers: It can contain only numeric value eg. 100, 102.5 .

Java Script makes no distinction between an integer value and a floating point value. All numbers are represented in Java Script as Numeric Data Type.

String of Text:  String type can contain string of text eg. “This is string Data Type”. String types are enclosed within the semicolons.

Boolean: Boolean returns true or false.

Variables:

Variables are containers for storing information. Java Script also have the facility of variables. Java Script Variables are used to hold the information. Before we declare variables in Java Script they have to be declared first. The variables are declared with the var keyword followed by the name of the variable.

<script type="text/javascript">

<!--

    var empName;

    var empAge;

-->

</script>

We can also declare multiple variables as follows

<script type="text/javascript">

<!--

    var empName, empAge;

-->

</script>

 

The above line declares multiple variables at the same line.

Variables Initialization:

Variables are initialized only when we are storing the values in the variable. Variables can be initialized at the time of its declaration or afterwards when the needs arise.

<script type="text/javascript">

    var empName = "Amit";

    var empAge = 20;

</script>

 

Java Script in Untyped Language means that in Java Script Variables can contain value of any data type Whether it is of integer or string or any other data type. We do not need to tell what type of value variable will be assigned, Java Script will take care of that automatically.

Redeclaration of Variables:

Redeclaration of Variable in Java Script will not loose previous variable value.

For eg.

  var carname = "Maruti";

  var carname;

 

This will retain the previous variable value maruti in the variable named carname.

Java Script Variable scope :

The variable which are declared in the Java Script are of two types:

1.                   Global Variable

2.                   Local Variable

Global Variable retain there values throughout the life time of a program.

But Local Variable retain there value till the function in which it is defined exist.

 

<script type="text/javascript">

<!--

    var myName = "Abhishek";     // This is a global variable

    function checkscope() {

        var myName = "Amit";     // This is a local variable

        document.write(myName);  // It will print myName as Amit  

    }

-->

</script>

 

Conclusion:

Through this article we came to know actually which data types does Java Script supports and how to declare them and how to compute value. We also learned how to declare variables.


Don't want to miss updates? Please click the below button!

Follow MindStick