Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Built-in javascript constructors?
In this article, we are going to discuss about the Built-in JavaScript constructors with appropriate examples in JavaScript.
Javascript has provided some built-in constructors for native objects. These built-in functions include Object(), String(), Boolean(), RegExp(), Number(), Array(), Function(), Date().
Note We cannot include Math object in those built-in constructors because Math is a global object. The 'new' keyword cannot be used with Math.
Let's understand this concept in a better way with the help of examples further in this article.
Example 1
This is an example program to illustrate about the inbuilt constructor String().
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>String() - Built-in JavaScript Constructor</h3>
<p id="valueToString"></p>
<script>
var value1 = 3.14;
var value2 = String(value1);
var result = value1;
document.getElementById("valueToString").innerHTML = "The converted value to string is : " + result;
</script>
</body>
</html>
On executing the above code, the below output is generated.
Example 2
This is an example program to illustrate about the inbuilt constructor Number().
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Number() - Built-in JavaScript Constructor</h3>
<p id='Built-in'></p>
<script>
let a = Number("100");
let b = Number("100.234");
let c = Number(true); //return 0 or 1 for boolean
let d = Number("true"); //returns a number or NaN
let e = Number(new Date()); //returns number of milli seconds from Jan 1 1970
let f = Number([1.2]);
document.getElementById('Built-in').innerHTML = `Number("100") is ${a} ${'<br/>'} Number("100.234") is ${b} ${'<br/>'} Number(true) is ${c} ${'<br/>'} Number("true") is ${d} ${'<br/>'} Number(new Date()) is ${e} ${'<br/>'} Number([1.2]) is ${f} `;
</script>
</body>
</html>
On executing the above code, the below output is generated.
Example 3
This is an example program to illustrate about the inbuilt constructor Boolean().
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Boolean() - Built-in JavaScript Constructor</h3>
<p id="bool"></p>
<script>
var value1 = 0;
var value2 = '';
var value3 = null;
var value4 = 'Hello';
var value5 = 1234;
document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : " + Boolean(value1) + '<br/>' + "The boolean value for the value ('') is : " + Boolean(value2) + '<br/>' +"The boolean value for the value (null) is : " + Boolean(value3) + '<br/>' +"The boolean value for the value ('Hello') is : " + Boolean(value4) + '<br/>' +"The boolean value for the value (1234) is : " + Boolean(value5);
</script>
</body>
</html>
On executing the above code, the below output is generated.
Example 4
This is an example program to illustrate about the inbuilt constructor Object(). Every constructor is a part of Object Constructor.
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Built-in JavaScript Constructors</h3>
<p id="valueToString"></p>
<script>
const a1 = new String();
const a2 = new Number();
const a3 = new Boolean();
const a4 = new Object();
const a5 = new Array();
const a6 = new Date();
document.getElementById("valueToString").innerHTML = `a1 is ${typeof a1} ${'<br/>'} a2 is ${typeof a2} ${'<br/>'} a3 is ${typeof a3} ${'<br/>'} a4 is ${typeof a4} ${'<br/>'} a5 is ${typeof a5} ${'<br/>'} a6 is ${typeof a6} `;
</script>
</body>
</html>
On executing the above code, the following output is generated.