Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

JavaScript Essentials - Functions

Defining and using functions in JavaScript

Functions are reusable blocks of code that perform a specific task. This tutorial covers how to define and use functions in JavaScript, providing a foundation for creating modular and maintainable code.

Key Points:

  • Functions encapsulate reusable code.
  • They can take parameters and return values.
  • Understanding functions is essential for writing modular and maintainable code.

Defining Functions

Functions can be defined using function declarations or function expressions. Here is an example of each:


// Function declaration
function greet(name) {
    return 'Hello, ' + name + '!';
}

// Function expression
var greet = function(name) {
    return 'Hello, ' + name + '!';
};

console.log(greet('Alice')); // Output: Hello, Alice!
                

Calling Functions

Functions are called by their name followed by parentheses. If the function takes parameters, pass the values inside the parentheses. Here is an example:


function add(a, b) {
    return a + b;
}

console.log(add(2, 3)); // Output: 5
console.log(add(10, 20)); // Output: 30
                

Parameters and Arguments

Parameters are variables listed as part of the function definition. Arguments are the values passed to the function when it is called. Here is an example:


function multiply(a, b) {
    return a * b;
}

console.log(multiply(4, 5)); // Output: 20
                

Return Statement

The return statement is used to return a value from a function. Once a return statement is executed, the function stops executing. Here is an example:


function divide(a, b) {
    if (b === 0) {
        return 'Error: Division by zero';
    }
    return a / b;
}

console.log(divide(10, 2)); // Output: 5
console.log(divide(10, 0)); // Output: Error: Division by zero
                

Arrow Functions

Arrow functions provide a concise syntax for writing functions. They are always anonymous. Here is an example:


// Arrow function
var greet = (name) => {
    return 'Hello, ' + name + '!';
};

console.log(greet('Bob')); // Output: Hello, Bob!
                

Function Scope

Variables declared within a function are local to that function and cannot be accessed outside of it. Here is an example:


function sayHello() {
    var message = 'Hello, World!';
    console.log(message);
}

sayHello(); // Output: Hello, World!
console.log(message); // Error: message is not defined
                

Anonymous Functions

Anonymous functions are functions without a name. They are often used as arguments to other functions. Here is an example:


setTimeout(function() {
    console.log('This message is displayed after 2 seconds');
}, 2000);
                

Immediately Invoked Function Expressions (IIFE)

Immediately Invoked Function Expressions are functions that are executed immediately after they are defined. Here is an example:


(function() {
    console.log('This function is executed immediately');
})();
                

Summary

In this tutorial, you learned about defining and using functions in JavaScript. You explored function declarations, function expressions, arrow functions, parameters and arguments, the return statement, function scope, anonymous functions, and IIFE. Understanding these concepts is essential for writing modular and maintainable JavaScript code.