Type of objects in JavaScript
JavaScript has several kinds of objects, and knowing which kind you are dealing with tells you what it can do and where it comes from. Broadly, there are built-in objects defined by the language itself, host objects provided by the environment such as the browser or Node.js, and user-defined objects that you create in your own code. Everything that is not a primitive value is an object of some kind.
Primitives versus objects
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Primitives are simple, immutable values. Everything else, including arrays, functions, dates and plain {} objects, is an object. When you call a method on a primitive, such as 'hello'.toUpperCase(), JavaScript briefly wraps it in a String object so the method works, then throws the wrapper away.
Built-in objects from the language
These exist in every JavaScript environment, browser or server.
- Fundamental:
Object,Function,Symbol,Errorand its subtypes likeTypeError. - Numbers and maths:
Number,BigInt,Math(a plain object of helpers, not a constructor). - Text:
String,RegExp. - Collections:
Array,Map,Set,WeakMap,WeakSet, typed arrays. - Structured data:
JSON,Date. - Control flow:
Promise, iterators and generators.
Host objects from the environment
The browser adds objects for working with the page and the device: window, document, DOM elements, localStorage, fetch, navigator and many more. Node.js provides different ones, such as process, Buffer and modules like fs. Code that uses document therefore fails in Node with "document is not defined", and code that uses process does not work in a browser without a bundler.
User-defined objects
These are the objects you create. You can make them with literals, constructor functions, classes or Object.create, which the next lesson covers in detail.
// Built-in
const today = new Date();
const ids = new Set([1, 2, 2, 3]); // Set {1, 2, 3}
const config = new Map([['theme', 'dark']]);
// Host (browser)
const title = document.querySelector('h1');
// User-defined
const cart = { items: [], total: 0 };
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
}
const pen = new Product('Pen', 20);
// Telling them apart
Array.isArray([]); // true
today instanceof Date; // true
Object.prototype.toString.call(ids); // "[object Set]"
pen.constructor.name; // "Product"
Object.prototype.toString.call(value) is a reliable way to get an internal type label for built-in objects, since typeof returns just "object" for most of them.
Wrapper objects: a trap to avoid
You can create objects that wrap primitives with new String('hi'), new Number(5) or new Boolean(false). Avoid this. A wrapper is an object, so typeof new Number(5) is "object", new Number(5) === 5 is false, and, most confusingly, new Boolean(false) is truthy because every object is truthy. An if check on it always passes.
Calling the same functions without new is fine and useful: Number('42') converts a string to the number 42, and String(42) gives '42'.
Choosing the right built-in
| Need | Use |
|---|---|
| Ordered list | Array |
| Unique values | Set |
| Key-value pairs with any key type | Map |
| Fixed record of named fields | Plain object |
Main points
Objects come from the language, the environment or your own code. Use built-ins like Map and Set when they fit, remember host objects differ between browser and Node, and never create primitives with new.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.