JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

JavaScript – Data Types and Variables

March 27, 2014 by Krishna Srinivasan Leave a Comment

JavaScript Data Types

JavaScript provides set of data types to hold data such as String values, Decimal values and Boolean values. The data types depend on the values which are hold by the variable. The JavaScript data types acts as dynamically means same variable can be used as different types.

JavaScript contain following three primitive data types :

Number

We can write numbers in two types i.e. integer and float. Numbers can be written with or without decimals. They represent numeric values. The simplest type of number is an integer with no fractional component. Another type is floating point number which is assumed to have decimal point.

Example:

[code lang=”html”]
<script langauge="javascript">
var a=2;
var b=3;
var c=a+b;
document.write ("Result :");
document.write(c);
</script>
[/code]

String

For string type, a value must be assigned in within single or double quotes. It is sequence of characters within given character set. Normally it is used to represent text. If we create a string with no value in it, then it is called as empty string.

Example:

[code lang=”html”]
<script langauge="javascript">
var a="hello world";
document.write(a);
</script>
[/code]

Boolean

It can have two boolean values: true and false. It’s evaluates whether condition meets or doesn’t meet specified criteria. In JavaScript, if need to use boolean values then it will convert automatically true to 1 and false to 0.While comparing the result, JavaScript will treat non zero value as true and zero value as false.

[code lang=”html”]
<script langauge="javascript">
var a=true;
if(a==true)
{
document.write("result is true");
}
</script>
[/code]

Composite Data Types

A composite data type for the purposes of JavaScript. It can contain multiple values grouped together in some way. It is same as object in the class. We can define attributes and methods to composite types. JavaScript contain following composite data types :

  • Object: An object is collection of values, which is referred to as properties of objects and functions associated with the object are referred to as methods of that object. The properties and methods are denoted with dot notation which starts with object name and ends with property name. For instance: emp.emp_name.
  • Array: It is ordered collection of data values which is used to store multiple data values into a single variable. In JavaScript, array is an object which contains index to refer to its contents. The array index is included in square brackets after array name and array index starts with zero. For instance: stud [0], stud [1] and so on.

Special Data Types

JavaScript contain following special data types :

Null

It is empty value. The null keyword cannot be used as name of the variable. In JavaScript null is not same as 0. It is absence of any value. If we try to reference a value for variable which is not defined and has no value then it returns a null value.
For instance:

[code lang=”html”]
var myTest=null;
alert(myTest);//shows null value
[/code]

Undefined

A variable declared without a value can be called as undefined. The undefined value is assigned to a variable to which value has never assigned to it.

Example:

[code lang=”html”]
if (a==undefined)
{
document.write ("comparing a to undefined ");
}
[/code]

JavaScript Variables

JavaScript variables are containers which hold some values or information. JavaScript variables are case-sensitive. We use variables to store, retrieve and manipulating values that appear in the code. We can place the data into container and refer to the data by naming container.

Variables are declared by using “var” keyword.

Example:

[code lang=”html”]
var a=5; –> Integer type variable
var str="javabeat"; –> String type variable
var b=true; –>Boolean type variable
[/code]

JavaScript Variable Scope

JavaScript variable has two scopes:

  • Global variables: A variable can be declared outside a function is called global variable.
  • Local variables: A variable which is defined within function definition is called local variable. It is created and destroyed every time when function is executed and it cannot be accessed by outside any of the function.

For Example:

[code lang=”html”]
var myTest; //global declaration
function result(){
var myTest1="hello world!!" ; //local declaration
}
[/code]

JavaScript Variable Names

  • JavaScript variable names should begin with a letter.
  • JavaScript variable names begin with $ or _ character. E.g. $123 or _123java
  • JavaScript variable names are case sensitive. E.g. Java and java are different variables.
  • We should not use reserved keyword as variable name.

Filed Under: JavaScript Tagged With: JavaScript Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved