LearnCode

Master Programming with Interactive Learning

Join our free courses and start coding like a pro today.

JavaScript Function - Part 2

Welcome to the second part of the JavaScript Function tutorial! πŸš€
Here, you'll explore advanced concepts like parameters, callbacks, closures, and more.

πŸ’‘ Tip: Press F12 to open the console and see live results of each example!

Function Parameters and Arguments

Functions can accept parameters, which act as placeholders for values. The actual values passed are called arguments.

function greet(name) {
  console.log("Hello, " + name + "!");
}
greet("Ayush"); // Output: Hello, Ayush!

Default Parameters

You can assign default values to parameters. If no argument is passed, the default will be used.

function greet(name = "Ayush") {
  console.log("Hello, " + name + "!");
}
greet(); // Output: Hello, Ayush!

Rest Parameters

Rest parameters allow you to handle an indefinite number of arguments as an array.

function sum(...numbers) {
  return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10

Callback Functions

A callback is a function passed into another function to be executed later.

function fetchData(callback) {
  setTimeout(() => {
    const data = { name: "Ayush", age: 20 };
    callback(data);
  }, 1000);
}
fetchData(function(data) {
  console.log("Data received:", data);
});

Arrow Functions

Arrow functions provide a concise syntax and can return values implicitly.

const multiply = (a, b) => a * b;
console.log(multiply(5, 3)); // Output: 15

Function Scope

Variables declared inside a function are accessible only within that function.

function myFunction() {
  let message = "Hello, World!";
  console.log(message); // Output: Hello, World!
}
myFunction();
console.log(message); // Error: message is not defined

Closures

Closures let a function access variables from its parent scope even after the parent function has finished.

function outerFunction(outerVariable) {
  return function innerFunction(innerVariable) {
    console.log("Outer Variable: " + outerVariable);
    console.log("Inner Variable: " + innerVariable);
  };
}
const newFunction = outerFunction("outside");
newFunction("inside");

Function Overloading

JavaScript doesn’t support function overloading natively, but similar behavior can be achieved using conditions.

function add() {
  let a = 5, b = 10;
  console.log("Sum:", a + b);
}
function add(a, b) {
  console.log("Sum:", a + b);
}
function add(a, b, c) {
  return a + b + c;
}
add();
add(15, 25);
console.log(add(10, 20, 30));

Function Declaration vs Expression

Declarations are hoisted; expressions are not.

// Declaration
console.log(square(5)); // βœ… Works
function square(x) { return x * x; }

// Expression
const squareExp = function(x) { return x * x; };
console.log(squareExp(5)); // βœ… Works only after declaration

🎯 In this lesson, you learned how to use function parameters, default values, rest parameters, callbacks, arrow functions, closures, and more. Keep practicing!