DEV Community

"Rocky" Hiroki Ueno
"Rocky" Hiroki Ueno

Posted on

JavaScript Types of Functions

Named function (Function declaration)

function helloWorld() {
   console.log('Hello World');
}

helloWorld();
Enter fullscreen mode Exit fullscreen mode

Named functions are used when the codes need to be executed several times.

Anonymous function

let helloWorld = function () {
   console.log('Hello World');
}

console.log(helloWorld);
Enter fullscreen mode Exit fullscreen mode

Anonymous functions are used the codes needs to be executed only once or twice and also used in event handler as below:

.onclick = function() {
   console.log('Hello World')
}
Enter fullscreen mode Exit fullscreen mode

Arrow function

const helloWorld = () => 'Hello World';

console.log(helloWorld);
Enter fullscreen mode Exit fullscreen mode

Arrow functions are shorter ways of function expressions from ES6 and useful to write functions in short ways.

Top comments (0)