Functions Notebook

Take Quiz

JavaScript Functions

Functions are the building blocks of readable, maintainable, and reusable code. In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions.

1. Function Declaration

The standard way to define a function.

IN [ ]
function greet(name) {
  return "Hello, " + name + "!";
}
console.log(greet("Alice"));

2. Function Expression

Assigning a function to a variable. These are not hoisted like declarations.

IN [ ]
const square = function(number) {
  return number * number;
};
console.log(square(4));

3. Arrow Functions (ES6)

A concise syntax for writing function expressions. They do not have their own this context.

IN [ ]
const multiply = (x, y) => x * y;
console.log(multiply(5, 10));

// Single line arrow functions have implicit return
const double = x => x * 2;

4. Default Parameters

Setting default values for arguments.

IN [ ]
function welcome(user = "Guest") {
  console.log(`Welcome, ${user}`);
}
welcome();
welcome("Rahul");