Arrow functions

Arrow functions allows us to write shorter syntax. Here's an example:

const hello = function() {
  return 'Hello!';
}

would become

const hello = () => {
  return 'Hello!';
}

or

const hello = () => 'Hello!';

However, you can only omit the brackets ({}) and return if the function is on a single line. As a best practice and for consistency, doing the following is recommended:

const hello = () => { return 'Hello!'; }

Using it in our code

For our code, you might have noticed that we have already used arrow functions. For example, take the following:

bot.on("ready", () => { // When the bot is ready
    console.log("Ready!"); // Log "Ready!"
});

Without arrow functions, we have to do:

bot.on("ready", function() { // When the bot is ready
    console.log("Ready!"); // Log "Ready!"
});

Here's another example:

bot.on("messageCreate", (message) => {
  // ...
});

would become

bot.on("messageCreate", function(message) {
  // ...
});

You see? Without arrow functions, our code would be much longer.

Copyright © Dusty 2021 - 2021