In this post, we will list some built-in function in JavaScript which can be used as constructor function.

 

1. Object()

 

This function can be used as a constructor to create object. Infact, these two lines are same:

 

var someObj = {}

 

var someObj = new Object();

 

That is the reason why objects created using object literal notation returns Object() as its constructor. We saw such an example in one of the previous post on Constructor functions.

 


 

2. Array

 

The following two ways of defining an array are same:

 

var arr = []

 

var arr = new Array();

 

Passing values to Array() function.

 

var arr = new Array(1,2,3,4);
console.log(arr); //returns "Array [ 1, 2, 3, 4 ]"

console.log(arr.length); //returns 4 

 

Pushing and Popping in an array:

 

var arr = new Array(1,2,3,4);
console.log(arr); //Array [ 1, 2, 3, 4 ]

//Push
arr.push('this is five')
arr.push(6);

//print the array again
console.log(arr); //Array [ 1, 2, 3, 4, "this is five", 6 ]

arr.pop();

console.log(arr); //Array [1, 2, 3, 4, "this is five"]

arr.pop()

console.log(arr); //Array [1, 2, 3, 4]

 

More operations on JavaScript array: join and slice

 

var arr = new Array(1,2,3,4);
console.log(arr); //Array [ 1, 2, 3, 4 ]

joinedArr1 = arr.join('-')
joinedArr2 = arr.join(' is joined with ')

console.log(joinedArr); //1-2-3-4
console.log(joinedArr2);//1 is joined with 2 is joined with 3 is joined with 4

 

Array slice property slices an array, which means it returns a part of the array. It never modifies the original array, just like join.

 

var arr = new Array(1,2,3,4,5,6,7,8,9);
console.log(arr); //Array [ 1, 2, 3, 4, 5, 6, 7, 8, 9]

slicedArr1 = arr.slice(1,3);
console.log(slicedArr1);

slicedArr2 = arr.slice(4,7);
console.log(slicedArr2);

 

Observe that the last index is not included in the new list.

 


 

3. Math()

 

Math cannot be used as a constructor function. It means you cannot use new with Math().

 

Math is a built-in global object. It is not a normal function. Its properties has fixed value and they cannot be altered.

 

//Value of PI
console.log(Math.PI); //3.141592653589793
//Square Root of 2
console.log(Math.SQRT2); //1.4142135623730951
//Natural Log of 2
console.log(Math.LN2); //.6931471805599453
//2 raised to power of 10
console.log(Math.pow(2,10)); //1024

 

How to get a random number in JavaScript?

 

Math.random() returns a random number between 0 and 1. To get a random number between two numbers m and n, where m>n, we use the following code:

 

console.log(((m-n)*Math.random())-n); 

 

To get the nearest integer, you can use:

 

console.log( Math.round( ((m-n)*Math.random())-n ) )

 

You can also use, Math.ceil() or Math.floor() to round up or round down respectively.

0 Do You Love This Article ?

CT

Hello All! You are reading the author(s) profile of CorvineLabs Team. We may be 1,2 or ..any in number. Let that remain withing us. You enjoy our write up. If you want to join the team than do Contact Us.

Leave a Reply

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