Introduction to JavaScript : Datatypes

Datatypes

Different data types are supported by JavaScript for the presentation of data as well as for performing various calculations needed for solving programming problems. The following are the supported datatypes: 

Strings 

Arrays 

Numbers 

Boolean  

JavaScript Strings 

A string stores a series of characters. JavaScript can automatically convert between string primitives and String objects, and therefore any helper method designed for a String object can be used on a string primitive. 

The syntax of creating a string in JavaScript is given below  

var st = new String("Teachsomebody"); 

Strings have some common properties like a constructor which provides a reference to the string function from which the object is being created, length() property providing the number of characters or length of the string and also the Prototype() property which enables the addition of procedures and properties to the object. 

Helper methods for both string objects and string primitives are already discussed in the JavaScript built-in functions section. Review the previous section for further details. 

JavaScript Arrays 

Arrays are for storing multiple instances or values by a single variable. Arrays store a collection of values, characters or elements having the same type in a fixed-size successive pattern. The following syntax presents a simple way of creating a new array object

var vegetables = new Array( "tomatoes", "carrots", "onions" );

or by using square brackets:

var vegetables = [ "tomatoes", "carrots", "onions" ];

Arrays also have some properties like a constructor which provides a reference to the array function from which the object is being created, length() property providing the number of elements of the array and also the Prototype()property which enables the addition of procedures and properties to the object. A list of the methods of the Array object along with their descriptions are given in the section of built-in functions.  

JavaScript Numbers  

The Number object play a very important role in JavaScript programming as they are used to present numerical data like integers, natural numbers or floating-point numbers.

Given below is the syntax for creating a number object  

var val = new Number(number);

JavaScript Boolean 

The Boolean Object represent only two values which are either true or false or can be represented as 1 or 0. If the value parameter does not consist of any valid value, then the object holds the initial value of false.

The syntax of creating a Boolean object is given below

var x = new Boolean(value);

The two properties of the Boolean object are constructor() and prototype() similar to the global objects as explained above. 


course logo
Introduction to JavaScript
Number of sections:
10
Content length:
Delivery formats: