Functions in JavaScript
• • A function is a block of reusable code.
• • Used to perform a specific task.
• • Improves code reusability, modularity,
readability.
• Syntax:
• function functionName(parameters) {
• // code block
• return value;
Types of Functions
• 1. Function Declaration:
• function greet() { [Link]("Hello!"); }
• 2. Function Expression:
• const greet = function()
{ [Link]("Hi!"); };
• 3. Arrow Function (ES6):
• const add = (a, b) => a + b;
Advantages of Functions
• • Reusability → Write once, use many times.
• • Modularity → Break complex tasks into
smaller parts.
• • Readability → Cleaner and organized code.
• • Scope Management → Keeps variables local
to functions.
• Example:
• function square(x) { return x * x; }