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

or

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

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Check Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog