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("vikaskumar2299@gmail.com is not a mail? Is vikas@xmail.com ? No? google.com? sachin@gmail.comsds gmail.com");
</script>

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

  • 1
    Possible duplicate of [Why does typeof array with objects return "Object" and not "Array"?](http://stackoverflow.com/questions/12996871/why-does-typeof-array-with-objects-return-object-and-not-array) – JJJ Jun 25 '16 at 08:12
  • 1
    Yes, arrays are just (special) objects. Any value that is not a primitive value is an object. – Felix Kling Jun 25 '16 at 08:12
  • 2
    For the second question, [read the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Properties_2). – JJJ Jun 25 '16 at 08: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. http://stackoverflow.com/a/27986285/4543207 – Redu Jun 25 '16 at 08:52

2 Answers2

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

printfmyname
  • 983
  • 15
  • 30
  • 1
    FWIW, `typeof` is a poor solution to get the *real* type of a value. – Felix Kling Jun 25 '16 at 08:16
  • Going through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof 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. – printfmyname Jun 25 '16 at 08:22
-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

Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21