how to create an object javascript code example

Example 1: creating an object javascript

let obj = {
// fields
  name:"value",
  num: 123,
//methods
  foo: function(){}
  
}

Example 2: objects in javascript

let object = {
  'key1': 'value1',
  'key2': 'value2',
  'keyn': 'valuen',
};
console.log(object);

Example 3: objects in javascript

let car = {
  engineNumber: 1234
  brand: 'BMW',
  break: function (){}
}

Example 4: how to create a object in javascript

var a = {
name: "aakash",
  age:30
}

Example 5: js object.create example

function userCreator (name, score) {
	const newUser = Object.create(userFunctionStore);
  	newUser.name = name;
  	newUser.score = score;
  	return newUser;
};

const userFunctionStore = {
  	increment: function() {
      	this.score++;
    },
  
  	login: function() {
     	console.log("Logged in!"); 
    }
};

const user1 = userCreator("Justin", 41);
const user2 = userCreator("Rainer", 5);
user1.increment();
console.log(user1.score) // 42

Example 6: how to create an object in javascript

const person = {
  name: 'Anthony',
  age: 32,
  city: 'Los Angeles',
  occupation: 'Software Developer',
  skills: ['React','JavaScript','HTML','CSS']
};

//Use Template Literal to also log a message to the console
const message = `Hi, I'm ${person.name}. I live in ${person.city}.`;
console.log(message);

Tags:

Java Example