JS: Create/Delete Property

By Xah Lee. Date: . Last updated: .

Properties can be created at the same time when you create a object. There are 2 ways:

  1. {k1:v1, k2:v2, …} [see JS: Object Literal Expression]
  2. Object.create(…) [see JS: Create Object]

Add Property

New properties can be added to a existing object by:

obj.k = v


obj[k] = v

This will create or modify a property for the object.

[see JS: Property Dot Notation / Bracket Notation]

Other ways to add/modify property are:

  1. Reflect.set(obj, k) → Create or modify a property, function form. [see JS: Reflect.set]
  2. Object.defineProperty(…) → Create or modify a property and its attributes. [see JS: Object.defineProperty]
  3. Object.defineProperties(…) → Create or modify multiple properties at once. [see JS: Object.defineProperties]
// adding a new property

// creating object with property "k1" and "k2"
const obj = {"k1":1, "k2":2};

// adding a property
obj["p3"] = 3;

console.log( obj ); // { k1: 1, k2: 2, p3: 3 }

Delete Property

  1. delete obj[key] [see JS: Delete operator]
  2. Reflect.deleteProperty (obj,key) [see JS: Reflect.deleteProperty]

JS Object Property Topic

  1. JS: Property Overview
  2. JS: Property Key
  3. JS: Property Dot Notation / Bracket Notation
  4. JS: Create/Delete Property
  5. JS: Get Property, Set Property
  6. JS: Check Property Existence
  7. JS: Access Property
  8. JS: Property Attributes, writable, enumerable, configurable
  9. JS: Getter/Setter Properties
  10. JS: Property Descriptor
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.