Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Constructor Property

The constructor property returns the constructor function for all JavaScript variables and shows their data type.


document.getElementById("demo1").innerHTML =
"john".constructor + "<br /> +
(3.14).constructor + "<br />" +
false.constructor + "<br />" +
[1,2,3,4].constructor + "<br />" +
{name:'john', age:34}.constructor + "<br />" +
new Date().constructor + "<br />" +
function () {}.constructor;
    
hello

 

You can check the constructor property to find out if an object is an Array (contains the word "Array").


var myArray = ["bob","james"];
function isArray(myArray)
{
    return myArray.constructor.toString().indexOf("Array") > -1;
}
$("#demo2").html(isArray(myArray));
    

 

You can check the constructor property to find out if an object is an Array (contains the word "Date").


var myDate = new Date();
function isDate(myDate)
{
    return myDate.constructor.toString().indexOf("Date") > -1;
}
$("#demo3").html(isDate(myDate));
    

https://www.w3schools.com/jsref/jsref_constructor_string.asp
https://www.w3schools.com/jsref/jsref_constructor_array.asp