JavaScript Functions Explained for Beginners 2026 | Declaration, Parameters, Arrow Functions with Examples

Author: Ritika
0

 

If you are learning JavaScript then one concept that you will see everywhere is functions. Whether you are building a simple calculator, a form validation system or a big project like a website dashboard, functions are always used.

 

When I first started learning JavaScript during my B.Tech, I honestly found that functions is a little confusing. Many students also get confused here because there are different ways to create functions in JavaScript. But once you understand the basic idea then it becomes very simple.

 

In simple words, a function is a block of code that performs a specific task. Instead of writing the same code again and again we can write it once inside a function and use it whenever we need.

 

 

For example, suppose you are making a college project and you need to calculate the total marks of students many times. Instead of repeating the same logic everywhere, you can create a function.

 

So basically, functions help in:

  • Reusing code
  • Making programs easier to read
  • Organizing large projects
  • Reducing errors

 

In this article, I will explain JavaScript functions in a very simple way, just like we discuss topics with friends before exams or while doing assignments.

 

(toc) #title=(Table of Content)

 

Function Declaration and Expression

In JavaScript, there are mainly two common ways to create functions:

  • Function Declaration
  • Function Expression

 

Many beginners think they are the same. They are similar but there are small differences.

Let’s understand both step by step.

 

Function Declaration

A function declaration is the most basic and traditional way to create a function.

Syntax

function functionName() {
   // code
}

 

Example

function sayHello() {
   console.log("Hello Students");
}

 

Now if we call this function:

sayHello();

 

Output:

Hello Students

 

Basically, what happened here?

  • We created a function named sayHello
  • Inside the function we wrote some code
  • When we call the function, the code runs

 

When I learned this first time in my Web Development lab, it felt very useful because we could reuse code easily.

 

Real life Example

Suppose you are making a student result system.

Instead of calculating grade every time you can create a function:

 

function calculateGrade(marks) {
   if(marks >= 90) return "A";
   else if(marks >= 75) return "B";
   else return "C";
}

 

Now whenever you need the grade:

calculateGrade(85);

 

Very simple and clean.

 

Function Expression

Now comes function expression.

Here we store a function inside a variable.

Syntax

let variableName = function() {
   // code
};

 

Example

let greet = function() {
   console.log("Good Morning");
};

 

Calling the function:

greet();

 

Output:

Good Morning

 

Many students ask: Why use function expression if function declaration already exists?

Good question.

Function expressions are useful when:

  • We want to pass functions as values
  • We want to control when functions are created
  • Used heavily in modern JavaScript and frameworks

 

Function Declaration vs Function Expression

Feature Function Declaration Function Expression
Syntax Uses function name() Stored inside a variable
Hoisting Works before declaration Does not work before declaration
Usage Simple programs Advanced JavaScript

 

Many students get confused about hoisting but for beginners just remember:

Function declaration can be called before writing it in code, while function expression cannot.

 

Parameters and Return Values

Now let's talk about something very important in functions:

  • Parameters
  • Return values

Without these, functions will not be very useful.

 

Parameters

Parameters are inputs given to a function.

They allow functions to work with different values.

Example

function addNumbers(a, b) {
   console.log(a + b);
}

 

Here:

a and b are parameters

Calling the function:

addNumbers(5, 10);

 

Output:

15

 

When I was practicing JavaScript problems on coding platforms then parameters helped a lot because we could pass different values each time.

 

Another Practical Example

Suppose you are building a student attendance system.

function showStudent(name) {
   console.log("Student Name: " + name);
}

 

Calling:

showStudent("Rahul");
showStudent("Ankit");

 

So one function can work with many values.

 

Return Values

Sometimes we want the function to send back a result.

For that we use return.

Example

function multiply(a, b) {
   return a * b;
}

 

Calling:

let result = multiply(4,5);
console.log(result);

 

Output:

20

 

Basically:

  • Function calculates something
  • return sends result back
  • We store it in a variable

When working on mini projects and assignments,the return values are extremely useful.

 

Arrow Functions

Arrow functions are a modern feature in JavaScript (ES6).

When I first saw arrow functions, I thought they looked strange. But later I realized they are actually shorter and cleaner.

Syntax

const functionName = () => {
   // code
}

 

Example

const sayHi = () => {
   console.log("Hi Students");
}

 

Calling:

sayHi();

 

Output:

Hi Students

 

Same functionality but syntax is shorter.

 

Arrow Function with Parameters

const add = (a,b) => {
   return a + b;
}

 

Calling:

console.log(add(3,7));

 

Output:

10

 

Short Arrow Function

const square = x => x * x;

 

Calling:

console.log(square(5));

 

Output:

25

 

When we start using React.js or modern JavaScript frameworks, arrow functions are used everywhere. So it is good to practice them early.

 

Examples

Let’s see some simple examples that beginners usually practice.

Example 1: Function to Calculate Average Marks


function averageMarks(a,b,c) {
   return (a+b+c)/3;
}

let avg = averageMarks(80,75,90);
console.log(avg);

 

Output:

81.66

 

This type of logic is often used in student result systems.

 

Example 2: Check Even or Odd Number

function checkNumber(num) {
   if(num % 2 === 0)
      return "Even";
   else
      return "Odd";
}

 

Calling:

console.log(checkNumber(10));

 

Output:

Even

 

Example 3: Arrow Function for Greeting

const greetStudent = (name) => {
   return "Hello " + name;
}

 

Calling:

console.log(greetStudent("Amit"));

 

Output:

Hello Amit

 

Example 4: Function in a Project

Suppose in your college project you need to calculate total price.

function totalPrice(price, quantity) {
   return price * quantity;
}

 

Calling:


let bill = totalPrice(200,3);
console.log("Total Bill:", bill);

 

Output:

Total Bill: 600

 

Common Mistakes Beginners Make

When learning functions many students make small mistakes. I also made these when practicing JavaScript.

Some common ones are:

  • Forgetting to call the function
  • Missing return keyword
  • Confusing parameters and arguments
  • Not understanding arrow function syntax

 

But don't worry. With practice it becomes very natural.

My suggestion for beginners:

  • Practice small programs daily
  • Use functions in mini projects
  • Try solving coding problems

 

FAQs

What is a function in JavaScript?

A function is a block of code designed to perform a specific task. It runs only when it is called and helps reuse code multiple times.

What is the difference between parameters and arguments?

Parameters are variables defined in the function.

Arguments are actual values passed when calling the function.

function add(a,b)   → parameters
add(5,10)           → arguments
What is an arrow function?

Arrow functions are a shorter syntax for writing functions in JavaScript. They were introduced in ES6 and are commonly used in modern JavaScript applications.

Can a function return multiple values?

A function usually returns one value but we can return multiple values using arrays or objects.

Why are functions important in programming?

Functions help in:

  • Code reuse
  • Better organization
  • Easier debugging
  • Cleaner and readable code

 

Conclusion

JavaScript functions are one of the most important concepts in programming. Once you understand functions properly then writing JavaScript programs becomes much easier.

In simple way functions allow us to group code together and reuse it whenever needed. In this article we already discussed:

  • Function declaration
  • Function expression
  • Parameters and return values
  • Arrow functions
  • Practical examples

 

From my own learning experience during B.Tech, I can say that the best way to master functions is practice. Try writing small programs like calculators, student grade systems ond simple games. Slowly you will become comfortable with functions.

 

Many modern technologies like React, Node.js and full stack development depend heavily on functions. So learning them well will definitely help in future projects and interviews.

 

Just keep practicing, experiment with code and don’t worry if things look confusing at first. Almost every programmer felt the same in the beginning. With time everything becomes clear. 🚀

 

Read Also : JavaScript Explained (2026) – How It Works, Syntax & DOM

 

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !)#days=(20)

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. By clicking "Accept", you consent to our use of cookies. Privacy Policy Cookies Policy
Accept !
Breaking News | News |