Search within TutorialsPark

Javascript Datatypes


Javascript Datatypes are used to specify the basic elements of the language.


Javascript Datatypes

1. Programs perform operation on data assigned to it, Javascript datatypes defines the type of data such as numbers or characters which can be stored and manipulated within a program.

2. Datatypes supported by Javascript can be classified into two categories

Primitive Datatypes : Most simple form of datatypes. Types are Undefined , Null, Boolean, Number, String.

Complex Datatypes : Complex Datatypes comprise of more than one components. It can be objects, array and functions.


Javascript Operator : typeof

1. The datatype of a given variable can be found out using the operator typeof .When used it can return one of the following string

undefined : If the datatype value is undefined.

boolean : If the datatype value is boolean.

string : If the datatype value is string.

number : If the datatype value is number.

object : If the datatype value is an object.

function : If the datatype value is a function.

Example: Javascript - typeof Operator

Note: Javascript comments can be used to Disable Scripts from being executed Give it a TRY! »


Javascript Datatypes: Undefined

A variable declared using var but uninitialized, is assigned the special value undefined.

Though, Uninitialized variables are automatically assigned the value undefined, its recommended to always initialize variables.

Give it a TRY! »

Javascript Datatype: Null

The datatype null is used to declare a variable and then intentionally express absense of a value.Using the special value null.

This datatype has only one value, a value of null which denotes an empty object pointer. Hence , a typeof returns an "object" when assigned a null value

Example: Javascript Datatype - Null

Note: When defining a variable to hold objects later, it recommended to initialize it to null. Give it a TRY! »


Javascript Datatype: Boolean

1. A boolean variable can hold only two values, its either true or false

Example: Javascript Datatypes- Boolean

Give it a TRY! » Note: Boolean literals are case sensitive.


Javascript Datatype: Number

Javascript uses the IEEE-754 format to hold double digit 64 bit binary format numbers.

The possible values for number can be both integer and floating point values including a special Not-a-Number(NaN), positive infinity and negative infinity.

Example: Javascript Datatype - Number

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript Datatype: String

Javascript has another basic datatype called the string. It consists of rows of 16 bit-unicode characters enclosed within either single quotes(') or double quotes("), but must be matched.

Example: Javascript Datatype - String

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript: Datatype Conversion

Javascript is a loosely typed language, if datatypes are mixed(i.e comparison is made between a number and string, Boolean and number, string and Boolean) even then calculations will be performed.

Javascript is builtin to decide on itself how to handle such expressions, but users can even force conversions of one type to another.

Javascript provides three functions in order to convert datatypes. They are as follows:

String() : To convert any datatype to string type

Number() : To convert any datatype to Number type

Boolean() : To convert any datatype to Boolean type

Example: Javascript - Datatype Conversion Functions

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript Datatype Conversion Function : parseInt()

Javascript function parseInt() accepts a string and converts it to an integer, it goes through each character of the input string and sees if it's a valid number.

If valid, its converted to integer, if not then the command stops and the function returns the number converted so far.

Example: Javascript - Datatype Conversion Function parseInt()

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript: Datatype Conversion Function - parseFloat()

Javascript function parseFloat() accepts a string and converts it to a floating point number, it goes through each character of the input string and sees if it's a valid floating point number.

If valid, its converted to a floating number, if not then the command stops and the function returns the number converted so far.

Example: Javascript - Datatype Conversion Function parseFloat()

Give it a TRY! » Note: If the number is null then the output is 0


Javascript: Datatype Conversion Function - eval()

Javascript function eval() function is used to evaluate a String as a Javascript expression and then returns the result of the evaluation

Example: Javascript - Datatype Conversion Function eval()

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Javascript: Datatype Conversion

Javascript is a loosely typed language, if datatypes are mixed(i.e comparison is made between a number and string, Boolean and number, string and Boolean) even then calculations will be performed.

Javascript is builtin to decide on itself how to handle such expressions, but users can even force conversions of one type to another.

Javascript provides three functions in order to convert datatypes. They are as follows:

String() : To convert any datatype to string type

Number() : To convert any datatype to Number type

Boolean() : To convert any datatype to Boolean type

Example: Javascript - Datatype Conversion Functions

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.