Reading Time 3
Number of Words 550
In JavaScript, a function is a reusable block of code designed to perform a specific task. Functions help make code more organized, efficient, and easy to maintain — especially when an action needs to be repeated multiple times.
A function has a name (chosen by the developer) and is defined using the keyword function. After the function name, parentheses () indicate that it’s a callable piece of code, and curly braces {} contain the statements that will be executed when the function is called.
Basic Function Structure
Here’s a simple example of a function definition and execution:
function sayHello() { console.log("Hello!"); } sayHello(); // Executes the function
In this example:
-
functionis the keyword that defines the function. -
sayHellois the name of the function. -
()indicates that it can receive parameters (in this case, none). -
{}encloses the code to be executed when the function runs.
Local and Global Variables in Functions
Variables defined inside a function are local, meaning they are only accessible within that function.
Variables defined outside any function are global, meaning they can be accessed anywhere in the program.
Example:
const message = 'Hello World'; // Global variable function sayHello() { console.log(message); // Access global variable const localMessage = 'Hello Local'; // Local variable console.log(localMessage); } sayHello(); // Executes the function console.log(message); // Works — global variable console.log(localMessage); // Error — local variable not accessible outside
Tip: Keep variable scope in mind. Local variables are safer to use inside functions because they don’t interfere with global variables.
Function Parameters
Parameters allow you to pass values into a function, making it reusable for different inputs. Parameters are defined inside parentheses () after the function name.
Example:
function sum(a, b) { console.log(a + b); } sum(2, 5); // Output: 7
You can reuse the same function with different arguments:
function sayHello(message) { console.log(message); } sayHello('Hello'); sayHello('Hi'); sayHello('H e l l o');
Returning Values with return
The return keyword allows a function to send back a result that can be used later in the program.
Example:
function greet(name = 'User') { return `Hello ${name}`; } console.log(greet()); // Output: "Hello User" console.log(greet('Maria')); // Output: "Hello Maria"
Here, if no argument is passed, the function uses the default parameter value 'User'.
Anonymous Functions
An anonymous function is a function without a name. Instead of defining it with function name(), it’s assigned directly to a variable. You can still pass parameters to it just like a regular function.
Example:
var sayHello = function(name = 'John') { return 'Hello ' + name; } console.log(sayHello()); // Output: "Hello John" console.log(sayHello('Jesus')); // Output: "Hello Jesus"
Arrow Functions (ES6 Syntax)
JavaScript also provides a shorter syntax for writing anonymous functions — called arrow functions.
Arrow functions are concise and often used for callbacks or small, simple functions.
Example:
var sayHello = (name) => `Hello ${name}`; console.log(sayHello('Albert')); // Output: "Hello Albert"
If your function includes more than one statement, use curly braces {} and an explicit return:
var sayHello2 = (name) => { return `Hello ${name}`; } console.log(sayHello2('Albert')); // Output: "Hello Albert"
Tip: Arrow functions don’t have their own this context, which makes them ideal for callbacks and event handlers, but not for methods inside objects.
Summary
| Concept | Description | Example |
|---|---|---|
| Function Definition | Creates a reusable block of code | function greet() { ... } |
| Function Call | Executes a function | greet() |
| Local Variable | Accessible only inside a function | let local = 'text' |
| Global Variable | Accessible anywhere in the code | let global = 'text' |
| Parameters | Input values for functions | function sum(a, b) |
| Return | Sends back a value from the function | return a + b |
| Anonymous Function | Function without a name | var x = function() {...} |
| Arrow Function | Shorter anonymous function syntax | (x) => x * 2 |