0

I'm working with this code. Now, just for checking, I printed typeof arr and it displayed "object" in the console. Although, I declared arr as an array in the beginning. Has it been converted to an object? Here's the code:

<script>
function getDomainsOfEmails(text) {
    var arr=new Array;
    var regex= /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}/g;
    var matches= text.match(regex);
    for(var i=0; i<matches.length; i++){
        arr[i]=matches[i].match(/@[a-z0-9.-]+\.[a-z]{2,4}/);
    }
    for(j=0; j<arr.length; j++){
    console.log(arr[j]);
    }
    console.log(typeof arr);
}
getDomainsOfEmails("[email protected] is not a mail? Is [email protected] ? No? google.com? [email protected] gmail.com");
</script>

Also, I would like to know if it's an object, what are its properties and values?

4
  • 1
    Possible duplicate of Why does typeof array with objects return "Object" and not "Array"? Commented Jun 25, 2016 at 8:12
  • 1
    Yes, arrays are just (special) objects. Any value that is not a primitive value is an object. Commented Jun 25, 2016 at 8:12
  • 2
    For the second question, read the documentation. Commented Jun 25, 2016 at 8:13
  • In JS an array is just a proper object. What makes it different like many other objects is it's constructor function (Array()) and it's prototype being by default set with many useful Methods. Positive integer properties designate the keys and their values designate the items of an array. There is a length property as well which is automatically gets updated accordingly through the keys getters and setters (i assume). You can take a proper object and by array sub-classing turn it into an array. stackoverflow.com/a/27986285/4543207 Commented Jun 25, 2016 at 8:52

2 Answers 2

3

Apparently yes according to following results

typeof Array()

returns "object"

Array() instanceof Object

returns true

Update: According to API documents

All Array instances inherit from Array.prototype. The prototype object of the Array constructor can be modified to affect all Array instances.

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Properties_2

Sign up to request clarification or add additional context in comments.

2 Comments

FWIW, typeof is a poor solution to get the real type of a value.
Going through developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I see that its actually better to use Array.isArray in your case to differentiate regular objects from arrays. So in this case typeof gives you undesirable result.
-1

yes Arrays are objects only

create an array

var arr = new Array();

arr.____proto____ (proto link stackoverflow is consuming 2 undescores) --> Array.prototype

Array.prototype ---> Object.prototype

Also [1,2,3] instanceof Object ---> true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.