DEV Community

Cover image for A Quick Dive into JavaScript Functions 🛠️
Noor Fatima
Noor Fatima

Posted on

A Quick Dive into JavaScript Functions 🛠️

Functions are one of the core features of JavaScript, helping you write reusable and organized code. Let's break down what functions are, how to create them, and how they differ from arrow functions.

What are Functions?

A function is a block of code that performs a specific task. Instead of writing the same code multiple times, you define a function once and call it whenever you need it.

How to Define a Function

  • Function Declaration: This is the traditional way to create a function.
function greet(name) {
    return "Hello, " + name + "!";
}

Enter fullscreen mode Exit fullscreen mode

Here, greet is the function name, and name is a parameter.

- Function Expression:

You can also define a function using a variable.

const greet = function(name) {
    return "Hello, " + name + "!";
};
Enter fullscreen mode Exit fullscreen mode

Conclusion
Understanding functions is fundamental in JavaScript. They allow you to organize and reuse code efficiently. By mastering function declarations and function expressions, you can write more maintainable and cleaner JavaScript code.

Top comments (0)