DEV Community

Anees Abdul
Anees Abdul

Posted on

Functions in JavaScript

What is a Function?
-A function is a block of code designed to perform a specific task.

  • Instead of writing the same code again and again, we can put it inside a function and reuse it.
function functionName() {
    // code to execute
}
Enter fullscreen mode Exit fullscreen mode
function greet() {
    console.log("Hello");
}

greet(); // calling the function
O/P: Hello
Enter fullscreen mode Exit fullscreen mode

Functions with Parameters

  • Parameters allow you to pass values into a function.
function greet(name) {
    console.log("Hello " + name);
}

greet("Anees");
O/P: Hello Anees
Enter fullscreen mode Exit fullscreen mode

Functions with Return Value

  • A function can return a value using return.
function add(a, b) {
    return a + b;
}

let result = add(2, 3);
console.log(result);
O/P: 5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)