• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

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:

<script langauge="javascript">
    var a=2;
    var b=3;
    var c=a+b;
    document.write ("Result :");
    document.write(c);
</script>

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:

<script langauge="javascript">
   var a="hello world";
   document.write(a);
</script>

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.

<script langauge="javascript">
   var a=true;
   if(a==true)
   {
      document.write("result is true");
   }
</script>

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:

    var myTest=null;
    alert(myTest);//shows null value

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:

if (a==undefined)
{
     document.write ("comparing a to undefined ");
}

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:

var a=5;		 --> Integer type variable
var str="javabeat";     --> String type variable
var b=true;		 -->Boolean type variable

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:

var myTest; //global declaration
function result(){
     var myTest1="hello world!!" ; //local declaration
}

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.

Category: JavaScriptTag: 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.

Previous Post: « JSF 2 Resource Bundle Example
Next Post: JSF 2 Message and Messages Example »

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact