Understanding JavaScript Functions 




 Introduction

JavaScript is the heart of front-end logic.
Two of its most important concepts are Functions and Arrays.
Understanding these makes you a confident developer who can write clean, reusable, and efficient code.

“Functions organize your logic. Arrays organize your data.”

JavaScript Functions

 What is a Function?

A function is a block of code designed to perform a specific task.
It runs when it is called or invoked.

Function Syntax:
function functionName(parameter1, parameter2) {
  // code to be executed
  return result;
}



Example 1 — Simple Function

function greet() {
  console.log("Welcome to JavaScript!");
}

greet(); // Output: Welcome to JavaScript!

Explanation:

  • function greet() → defines the function

  • greet(); → calls the function
Example 2 — Function with Parameters

function calculateSalary(basic, bonus) {
  return basic + bonus;
}

console.log(calculateSalary(30000, 5000)); // Output: 35000

Here,

  • basic and bonus are parameters (inputs).

  • The function returns total salary.
Example 3 — Arrow Function (Modern JS)

const addNumbers = (a, b) => a + b;

console.log(addNumbers(5, 10)); // Output: 15

Arrow functions are shorter and cleaner.
Used widely in React (like event handlers, useEffect, etc).

Example 4 — Function Calling Another Function

function getHRA(basic) {
  return basic * 0.3;
}

function calculateTotalSalary(basic) {
  const hra = getHRA(basic);
  return basic + hra;
}

console.log(calculateTotalSalary(20000)); // Output: 26000
You can call one function inside another to organize calculations.

Conclusion — JavaScript Functions

avaScript functions are the core building blocks of any web application.
They help you write reusable, structured, and clean code that performs specific tasks efficiently.

In Simple Words:

A function is a small, independent block of code that takes inputs (parameters),
performs an operation, and returns an output — used again and again wherever needed.


Key Points Summary

  • Functions reduce code repetition and improve maintainability.

  • They can be named, anonymous, or arrow functions depending on the use case.

  • Functions can also be passed as arguments (callbacks) or return other functions (higher-order).

  • Modern JavaScript (especially React) heavily depends on arrow functions and functional patterns.

  • Writing clear, modular functions makes your code readable, testable, and scalable.

 
























 

 

 

Comments

Popular posts from this blog

SOLID PRINCIPLES IN JAVA SCRIPT