JavaScript Data Types

JavaScript Data Types:

In this article, I am going to discuss JavaScript Data Types in detail. Please read our previous article where we discussed the JavaScript variable. Before going with JavaScript data types will try to understand what data type is?

What is a Data Type?

As we all know the computers are machines that process data. Data is stored in variables (as discussed earlier). Variables have some unique characteristics such as:

  • Name
  • Data type
  • Value

A data type defines the type of information stored in the computer memory (using a variable).

Examples:
  • Positive integers: 1, 2, 3 …
  • Alphabetical characters: a, b, c…
  • Dates from the calendar: 19-Jan-2019 and so on.
JavaScript Data Types:

JavaScript is a typeless language that means

  • The variable types are not explicitly defined
  • The type of variable can be changed at runtime.

As we discussed earlier variables in JavaScript are declared with the var keyword.

Data types in JavaScript are dynamic.

That means JavaScript data types are converted automatically as needed during the script execution. In other words, we can say that the same variable can be used to hold different data type: In order to understand this please have a look at the following examples:

var count=1; //variable holds an integer value
count =hello; //the same variable now holds a string value
var name=’Shagufta’; //variable holds a string value
name=4.5; //variable holds a floating-point number value.

Primitive and Reference Value:
Primitive Values:
  • Primitive values are the simple piece of data that are stored on the stack.
  • That means the value is stored directly in the location from where the variable is accessed.
  • The JavaScript primitive types are Undefined, Null, Boolean, Number, or String.
  • Many languages consider string as a reference type and not a primitive type but JavaScript considers it a primitive type.
Reference Values:
  • Reference values are objects that are stored in heap.
  • That means the value stored in the variable location as a pointer to a location in memory where the object is stored.

In JavaScript, there are 3 primary data types, 2 composites (reference) data types, and 2 special data types.

The primitive data types are:

  • String
  • Number
  • Boolean

The Special data types are:

  • Undefined
  • Null

The composite (reference) data types are:

  • Object
  • Array
String data types:

It represents a sequence of characters enclosed with single quotes (‘) and double quotes (‘’) respectively.

Example:
var country=”India”;
var day=’Monday’;

Number data types:

The number of data types deal with digits. It covers both floating-point numbers as well as integers.

Floating-point numbers are like 4.22 and 0.98

Integer numbers are like 25, 125, and 1200

Example:
var age=23;
var radius=2.5;

Infinity and NAN:

Infinity is a property of a number.it represents mathematical infinity.

Example: var infinity=1e400*1250;

NaN stands for not a number and it’s a result of a mathematical operation that doesn’t have sense. This popup when we divide 0 by 0.

Example: Var x=0/0;

Boolean data types:

The Boolean type has two values, true and false.

Example:
var exist= true;
var profit=false;

Undefined data types:

Undefined has only one value Undefined. It means a variable has not been defined, no such variable exists in the current scope. In simple words, nothing is stored in the variable. Undefined is different from that null.

Example:
Var x;
alert(x);

In the above variable name x, no value is assigned to it.

Null data types:

The null type has only one value Null. It means that an object exists and is empty. In simple words, the null values mean no data or this variable doesn’t have any data stored in it.

Example:
Var x=null;
alert(x);

Object data type:

Everything is an object in JavaScript. JavaScript is an object-based language.

Example:
var objectname=new Object();  

Array data type:

The array is an ordered sequence of elements.

If we want multiple variables of the same or different types then remembering names of variables is a problem. The concept of the array allows us to store different data type data information under the same name in a sequence ordered way. It helps in creating many variables. There is no need for remembering their names because they all have the same name but different positions in the array. The counting in an array starts from 0, not from 1, means the first location is 0th location and 12th is 11th.

Example: Var b=new Array (“one”,”two”,”three”,”four”);

In the above example, we have declared an array variable.

In the next article, I am going to discuss JavaScript Operators in detail with example. Here, in this article, I try to explain JavaScript Data Types in detail. I hope you enjoy this article.

Leave a Reply

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