JavaScript Object initializer
Last Updated :
24 May, 2023
Objects in JavaScript can be compared to real-life objects. They have properties and methods attached to them and properties are in the form of key-value pairs. Let us understand this with an example. In the real world, a motorcycle is an object and it has properties like name, color, price, etc. It has some methods attached to it like start, brake, stop, etc. All motorcycles will have similar properties but the values will be different. This same concept is applied in programming and is known as Object Oriented Programming.
JavaScript objects can be initialized in various ways which are as follows.
Using Object Literals: Properties of JavaScript objects can be accessed using a dot notation or bracket notation. For Example, obj.name or obj['name'] will give us the value.
Syntax:
let obj = { name: "value", .... }
Example: This example shows the above-explained approach.
JavaScript
let person = {
name: "Sarah",
age: 20,
gender: "female"
};
console.log(person);
console.log(person.name + " is a " + person.age + "
year old " + person.gender);
console.log(person.name + " is a " + person.age + "
year old " + person["gender"]);
Output:
{name: 'Sarah', age: 20, gender: 'female'}
Sarah is a 20 year old female
Sarah is a 20year old female
Using new Object() method: The new Object() method will make a new JavaScript object whose properties can be initialized using dot or bracket notation.
Syntax:
let obj = new Object();
obj.name = "value";
or
obj["name"] = "value";
Example: This example shows the above-explained approach.
JavaScript
let Person = new Object();
Person.name = "Sarah";
Person['age'] = 20;
Person.gender = "female";
console.log(Person);
console.log(Person.name + " is a " + Person.age +
" year old " + Person.gender);
console.log(Person.name + " is a " + Person.age +
" year old " + Person["gender"]);
Output:
{name: 'Sarah', age: 20, gender: 'female'}
Sarah is a 20 year old female
Sarah is a 20year old female
Using Object.create() method: The Object.create() method will make a new JavaScript Object whose properties can be initialized using dot or bracket notation.
Syntax:
let Obj = Object.create({});
Obj.name = "value";
or
Obj["name"] = "value";
Example: This example shows the above-explained approach.
JavaScript
let Person = Object.create({})
Person.name = "Sarah";
Person["age"] = 20;
Person.gender = "female";
console.log(Person);
console.log(Person.name + " is a " + Person.age +
" year old " + Person.gender);
console.log(Person.name + " is a " + Person.age +
" year old " + Person["gender"]);
Output:
{name: 'Sarah', age: 20, gender: 'female'}
Sarah is a 20 year old female
Sarah is a 20year old female
Using Constructor functions: In this method, the constructor function is used to define the object and this is used to assign value to the properties. An instance of the object is created using a new keyword.
Syntax:
function Obj(name) {
this.name = name;
}
let myobj = new Obj("my name");
Example: This example shows the above-explained approach.
JavaScript
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
let personOne = new Person("Sarah", 20, "gender");
console.log(personOne);
console.log(personOne.name + " is a " + personOne.age +
" year old " + personOne.gender);
console.log(personOne.name + " is a " + personOne.age +
" year old " + personOne["gender"]);
Output:
{name: 'Sarah', age: 20, gender: 'female'}
Sarah is a 20 year old female
Sarah is a 20year old female
Supported Browser:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 4 and above
- Safari 1 and above
Similar Reads
JavaScript Object Constructors An object is the collection of related data or functionality in the form of key. These functionalities usually consist of several functions and variables. All JavaScript values are objects except primitives. const GFG = { subject : "programming", language : "JavaScript",}Here, subject and language a
4 min read
Default Constructor in JavaScript In JavaScript, a default constructor is not explicitly defined like in some other programming languages such as Java or C++. In JavaScript, objects can be created without a formal constructor. When you create an object using the new keyword along with a constructor function, that function serves as
2 min read
Constructor and Method Declarations in class (ES2015+) In the ES2015+ version of JavaScript, a class is a blueprint for creating objects, and it provides a way to define the structure and behavior of those objects in a clear and organized way. A class is defined using the class keyword, and it has two main parts: the constructor and the methods. Constru
6 min read
The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial
2 min read
Output of Java Program | Set 3 Predict the output of the following Java Programs: Example1: Java // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printin
3 min read
Java Constructors In Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall
10 min read
What is Constructor? A constructor is a special type of method used in object-oriented programming languages to initialize objects. The constructor is called automatically every time when an object is created, allowing the object to set initial values for its attributes or perform other setup tasks.In this article, we w
3 min read
Output of Java Programs | Set 50 Q 1. What is the output of this program? Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run time error : a is th
3 min read
new operator in Java When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process : Declaration : First, you must declare a variable of the class type. This vari
5 min read
Constructor newInstance() method in Java with Examples The newInstance() method of a Constructor class is used to create and initialize a new instance of this constructor, with the initialization parameters passed as parameter to this method. Each parameter is unwrapped to match primitive formal parameters, and both primitive and reference parameters ar
3 min read