Skip to content

Try our new Crash Courses!

Become a member using the button below to hide this banner and gain access to our new Crash Courses.

Arrow functions (no parameters) – JavaScript

Length: 10 minutes

Summary

() => {
  return 'hello world';
}

() => 'hello world';

Details

An arrow function is another way of writing a function expression.

To create an arrow function without arguments, first write a pair of parentheses, followed by a space and an equals sign and greater than sign right next to each other.

() => 

After the arrow, add a space and a pair of curly braces separated by a new line.

() => {

}

Put the statements you want inside the function.

() => {
  console.log('hello');
  console.log('world');
}

Realistically, you would store the arrow function expression inside a variable or constant or use it as a callback function.

let myFunction = () => {
  console.log('hello');
  console.log('world');
}

Simplifying the arrow function

() => {
  return 'hello world';
}

If the arrow function only contains a single return statement, like the example above, you can actually get rid of the curly braces and return keyword and put everything on one line, like this:

() => 'hello world';

Demo

Exercises

let myFunction = () => {
  console.log('hello');
}

myFunction()

References

Arrow function expressions on MDN

Back to: JavaScript Reference > JS Functions