Kruk87 Posted January 3, 2015 Share Posted January 3, 2015 I have a short code: <!DOCTYPE html><html><body><p id="demo"></p><script>var fruits = [];document.getElementById("demo").innerHTML = isArray(fruits);function isArray(myArray) { return myArray.constructor.toString().indexOf("Array");}</script></body></html> Can someone explain to me why this function returns 9 ? Link to comment Share on other sites More sharing options...
Don E Posted January 3, 2015 Share Posted January 3, 2015 myArray.constructor.toString() returns this string: function Array() { [native code] } and "Array" is at the 9 index when you have this: myArray.constructor.toString().indexOf("Array"); Link to comment Share on other sites More sharing options...
Kruk87 Posted January 4, 2015 Author Share Posted January 4, 2015 Lol, feel silly now that I didn't examine this close enough ( even if I am a total beginner in JS ). Thanks for the answer though. Link to comment Share on other sites More sharing options...
davej Posted January 5, 2015 Share Posted January 5, 2015 I'm curious what you were attempting to do. http://web.mit.edu/jwalden/www/isArray.html Link to comment Share on other sites More sharing options...
Don E Posted January 5, 2015 Share Posted January 5, 2015 Lol, feel silly now that I didn't examine this close enough ( even if I am a total beginner in JS ). Thanks for the answer though. No need to feel silly. We live and learn. Link to comment Share on other sites More sharing options...
davej Posted January 6, 2015 Share Posted January 6, 2015 But what sort of coding situation would have you processing unknown data structures? Link to comment Share on other sites More sharing options...
Don E Posted January 6, 2015 Share Posted January 6, 2015 It may not be it but the code looks like something from a w3schools tutorial he came across or a variation of one that he may have edited himself for understanding/learning. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now