#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

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, Error and its subtypes like TypeError.
  • 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

NeedUse
Ordered listArray
Unique valuesSet
Key-value pairs with any key typeMap
Fixed record of named fieldsPlain 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.

Explore Course