JavaScript

What is the Use of init() in JavaScript?

init()” is not a pre-built function or method in JavaScript. It is a popular naming convention for functions used to initialize or create an object or a script. It can be used to perform a variety of functions, including assigning default values, creating and initializing objects and variables, binding event listeners, preparing the environment required for a script to execute properly, and so on. Knowing how to use the init() function is essential for developing robust and efficient JavaScript applications.

This post will describe the use of init() in JavaScript.

What is the Use of init() in JavaScript?

init” stands for “initiate”. Generally, it is utilized to create a “new Object()”. The init() function is not a built-in feature of JavaScript but it is a naming convention that is utilized for designing a function and is responsible for initializing or setting up a program or application. In jQuery, the “init()” function is used to return a new “jQuery object”.

The init() function can be used in a variety of ways such as creating and initializing objects or classes, setting up global variables and constants that are utilized in the program, and so on.

Example
In JavaScript, we can create an object using a constructor call as we will do in the given example. First, create a class “StudentDetails”, and create a constructor in it with three parameters “name”, “age” and “grade”. Set the values of these parameters using the “this” keyword:

class StudentDetails {
 constructor(name, age, grade) {
  this.name = name;
  this.age = age;
  this.grade = grade;
 }
}

Create an object of the class using the “new” keyword that will call the constructor:

const student = new StudentDetails("Mili", 20, "A-Level");

Now, print the values of the object’s properties on the console:

console.log(student.name);
console.log(student.age);
console.log(student.grade);

Output

In JavaScript, the init() function can also be used as a constructor for creating an object. For this, define a function with the name init() and utilize the “Object.create()” method for creating a new instance of the object.

Here, we create a constructor using the “init” and for printing the values of the object’s parameters/properties, we will create a function “print”:

var StudentDetails = {
 init: function(name, age, grade){
  this.name = name;
  this.age = age;
  this.grade = grade;
 },
 print: function(){
  console.log(this.name);
  console.log(this.age);
  console.log(this.grade);
 }
};

Call the “Object.create()” method by passing the class “StudentDetails” as an argument to create a new object:

var student = Object.create(StudentDetails);

Now, invoke the “init” and “print” functions with the object. Here, “init” will help in the instantiation of object properties. In fact, init is a constructor call on the class “StudentDetails”:

student.init('Mili', 20, 'A-level');
student.print();

The output shows that the object has been successfully created and instantiated with its properties:

That’s all about the use of init() in JavaScript.

Conclusion

The init() function is not a built-in feature of JavaScript but it is a naming convention that is used for the designing of a function that is responsible for initializing or creating an object or a script. The init() function can be used in a variety of ways such as creating and initializing objects or classes, and setting up global variables and constants, and so on. This post described the usage of init() in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.