Function Declaration

Defining functions with the function keyword.

Examples

Basic Declaration

A simple function that takes a name and returns a greeting.

function greet(name) {
  return "Hello " + name;
}
console.log(greet("Alice"));

Hoisting Example

Function declarations are hoisted to the top of their scope.

console.log(square(5)); // Works, output: 25

function square(n) {
  return n * n;
}

No Return Value

Functions without a return statement return undefined.

function showMessage() {
  alert("This function returns undefined!");
}
let result = showMessage();
console.log(result); // undefined

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.