Javascript Basics

Javascript Different Types of Data Types

In previous chapter, we have learned by using identifier “var” we declare variable. By using assignment operator we assign data to the declared variable. Unlike Java, .Net, C# we do not require keyword to define the datatype. Javascript compiler can understand the datatype from the assigned value.

Now the question is, what kind of data we can store in a variable name?

Datatypes

Javascript has following types of datatypes

  • String
  • Numbers
  • Decimals
  • Boolean
  • Array
  • Object

 

String

In Javascript, we assign string value as below.

Example

<script>

var fruit_name=”Grape”;

</script>

We keep string value in between double inverted quotes.

Numbers

We assign number values to a variable as below.

Example

<script>

var employee_age=46;

</script>

Decimals

In javascript, we assign decimal values to a variable name as below.

<script>

var mobile_phone_price=4000.50;

</script>

Boolean

Boolean data has only two values – True or false. By assigning these values we declare boolean variable data types.

Example

<script>

var isItRaining=true;

var isDiwaliToday=false;

</script>

Note: The values true and false are not kept inside any quote. True and false are constant values. So whenever we want to declare boolean variable then we can assign its value with either true or with false.

Array

In javascript, we assign array values keeping these values in between square brackets.

Example

<script>

var fruitNames=[“apple”,”orange”,”grape”,”guava”];

</script>

What is an array?

An array is a variable in which we store similar kind of data. (In another chapter we are going to discuss about it in detail).

Object

Javascript object values are assigned using curly braces.(Object is an advance topic. We are going to discuss in detail in some other chapter)

<script>

var EmployeeData={name:”John Smith”,age:”50′,status:”ad-hoc”,salary:”level6″};

</script>

So, these are the different datatypes we use in Javascript.