Course contentsShow
← JavaScript
Lesson 976 of 2,72322. Spread and RestFree lesson

Spread and Non-Enumerable Properties

Learn that spread only copies enumerable own properties, ignoring non-enumerable and inherited ones.

Spread and Non-Enumerable Properties

What You'll Learn

You'll discover that the spread operator only copies enumerable own properties, meaning it silently ignores non-enumerable and inherited properties.

Understanding Property Enumeration

In JavaScript, object properties have internal flags. One of these flags is enumerable, which determines whether a property shows up in for...in loops or gets copied by spread. Most properties you create are enumerable by default, but some built-in properties and properties defined with special methods are non-enumerable.

Think of it like items in a store: enumerable properties are on display shelves (visible and accessible), while non-enumerable properties are in the back storage room (they exist, but aren't automatically shown to customers browsing).

How Spread Handles Non-Enumerable Properties

When you use spread ({...obj}) to copy an object, it only copies properties where enumerable: true. Any non-enumerable properties simply don't get transferred to the new object.

const original = { name: 'Alice' };

// Create a non-enumerable property
Object.defineProperty(original, 'secret', {
  value: 'hidden',
  enumerable: false
});

const copy = { ...original };

console.log(copy.name);   // 'Alice' (copied)
console.log(copy.secret);  // undefined (not copied!)
console.log(original.secret); // 'hidden' (still exists on original)

The secret property exists on original but doesn't appear in copy because it's non-enumerable.

Why This Matters

This behavior is usually what you want—built-in non-enumerable properties (like those on prototype chains) shouldn't be accidentally copied. However, if you need to copy all properties including non-enumerable ones, you'll need different tools like Object.getOwnPropertyDescriptors() (which you haven't learned yet).

Key Takeaway: Spread only copies enumerable own properties—non-enumerable and inherited properties are silently ignored during the copy process.