Functions in JavaScript

Javascript 8 min min read Updated: Mar 09, 2026 Intermediate
Functions in JavaScript
Intermediate Topic 1 of 10

Functions are one of the most important features in JavaScript. A function is a block of reusable code designed to perform a specific task. Instead of writing the same code multiple times, developers can place the code inside a function and call it whenever needed.

Functions help make programs more organized, readable, and efficient. They are widely used in web applications to perform tasks such as validating forms, handling user input, processing data, and performing calculations.

What is a Function?

A function is a named block of code that executes when it is called. It allows developers to reuse code and simplify complex programs.

javascript function greet(){ console.log("Hello, Student"); } greet();
Output

Hello, Student

In this example, the function named greet prints a message when it is called.

Function Syntax

The basic syntax of a function in JavaScript looks like this:

javascript function functionName(parameters){ // code to be executed }

A function contains:

  • The function keyword
  • The function name
  • Optional parameters
  • A block of code inside curly braces

Function with Parameters

Functions can accept input values called parameters. These parameters allow functions to work with different data each time they are executed.

javascript function greet(name){ console.log("Hello " + name); } greet("Rahul");
Output

Hello Rahul

Here, the parameter name receives the value "Rahul" when the function is called.

Function with Return Value

Functions can return values using the return statement. This allows the result of the function to be used elsewhere in the program.

javascript function add(a, b){ return a + b; } let result = add(5, 3); console.log(result);
Output

8

In this example, the function returns the sum of two numbers.

Function Expression

JavaScript also allows functions to be stored inside variables. This is known as a function expression.

javascript let greet = function(){ console.log("Welcome to JavaScript Tutorial"); }; greet();
Output

Welcome to JavaScript Tutorial

Arrow Functions

Arrow functions are a shorter syntax introduced in modern JavaScript (ES6). They allow developers to write functions in a more concise way.

javascript const add = (a, b) => { return a + b; }; console.log(add(4, 6));
Output

10

Why Functions are Important

Functions are essential in JavaScript because they help organize code into reusable blocks. Instead of repeating the same instructions many times, developers can define a function once and call it whenever needed.

Functions also make programs easier to maintain and debug. Large applications rely heavily on functions to structure their logic and perform different tasks efficiently.

Conclusion

Functions are a fundamental concept in JavaScript programming. They allow developers to create reusable code blocks that perform specific tasks. By using parameters and return values, functions can handle different inputs and produce useful results.

Mastering functions is important for writing efficient JavaScript programs and building dynamic web applications. In the next tutorial, you will learn about Scope in JavaScript, which explains how variables behave inside and outside functions.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators