Answers for "how to create object with key and value javascript"

8

javascript create object key from variable

//For ES6 and Babel
{
    [yourKeyVariable]: "yourValue",
}

// ES5 Alternative
// Create the object first, then use [] to set your variable as a key
var yourObject = {};

yourObject[yourKeyVariable] = "yourValue";
Posted by: Guest on September-28-2020
2

js create object with properties

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
Posted by: Guest on April-01-2020
0

js object using variable as key

let yourKeyVariable = "Fire";
{
    [yourKeyVariable]: someValue,
}

/* object will resolve to { Fire : someValue } */

/*
 This is similar to adding k-v pair to an object using the other syntax 
ie. object[yourKeyVariable] = someValue; 
*/
Posted by: Guest on November-04-2021

Code answers related to "how to create object with key and value javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language