Functon isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1;} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Functon isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1;}

please I don't understand this code...is under constructor func in J.S

25th Jan 2018, 7:03 PM
Android Boi
Android Boi - avatar
1 Answer
+ 17
The constructor property returns the function that created it. Example: var arr=[1,2]; alert(arr.constructor.toString())// function Array(){[native code]} This shows that the object is an array. The code you gave checks this only. Example 2: var num=5; alert (num.constructor.toString())//function Number(){[native code]} The .toString() method ensures that the function is converted to string and the indexOf() method checks the position in which the given value is. If it is not there, -1 is returned.
26th Jan 2018, 1:57 AM
Swapnil Srivastava
Swapnil Srivastava - avatar