javascript set object property code example

Example 1: add property to object javascript

let yourObject = {};

//Examples:
//Example 1
let yourKeyVariable = "yourKey";
yourObject[yourKeyVariable] = "yourValue";

//Example 2
yourObject["yourKey"] = "yourValue";

//Example 3
yourObject.yourKey = "yourValue";

Example 2: js create object with properties

var mycar = new Car('Eagle', 'Talon TSi', 1993);

Example 3: js create object with properties

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Example 4: how to change object property value in javascript

myObject.sProp = 'A new string value for our original string property';

Example 5: add property with value in js

var myObject = {
    sProp: 'some string value',
    numProp: 2,
    bProp: false
};

Tags:

Misc Example