Why does typeof array with objects return “object” and not “array”? [duplicate]

One of the weird behaviour and spec in Javascript is the typeof Array is Object. You can check if the variable is an array in couple of ways: var isArr = data instanceof Array; var isArr = Array.isArray(data); But the most reliable way is: isArr = Object.prototype.toString.call(data) == ‘[object Array]’; Since you tagged your question … Read more

Why is null an object and what’s the difference between null and undefined?

(name is undefined) You: What is name? (*) JavaScript: name? What’s a name? I don’t know what you’re talking about. You haven’t ever mentioned any name before. Are you seeing some other scripting language on the (client-)side? name = null; You: What is name? JavaScript: I don’t know. In short; undefined is where no notion … Read more

Type Checking: typeof, GetType, or is?

All are different. typeof takes a type name (which you specify at compile time). GetType gets the runtime type of an instance. is returns true if an instance is in the inheritance tree. Example class Animal { } class Dog : Animal { } void PrintTypes(Animal a) { Console.WriteLine(a.GetType() == typeof(Animal)); // false Console.WriteLine(a is … Read more