What is the use of the init() usage in JavaScript?

David S. picture David S. · Oct 25, 2011 · Viewed 206.9k times · Source

What is the meaning and usage of the init() function in JavaScript?

Answer

nnnnnn picture nnnnnn · Oct 25, 2011

JavaScript doesn't have a built-in init() function, that is, it's not a part of the language. But it's not uncommon (in a lot of languages) for individual programmers to create their own init() function for initialisation stuff.

A particular init() function may be used to initialise the whole webpage, in which case it would probably be called from document.ready or onload processing, or it may be to initialise a particular type of object, or...well, you name it.

What any given init() does specifically is really up to whatever the person who wrote it needed it to do. Some types of code don't need any initialisation.

function init() {
  // initialisation stuff here
}

// elsewhere in code
init();