/03. Functions & Scope

    03. Functions & Scope

    Explanation

    Functions represent reusable blocks of code that perform a specific task, helping you follow the DRY (Don't Repeat Yourself) principle. They take inputs called parameters and can return an output value using the `return` keyword for further use in your app. Scope determines where your variables are accessible; variables declared inside a function are "local" to that function. Global variables, conversely, can be accessed from anywhere but should be used sparingly to prevent naming conflicts. Modern JavaScript favors arrow functions for their concise syntax and the way they handle the `this` keyword context. Defining clear, single-purpose functions makes your codebase much easier to test, debug, and maintain over time.

    Example

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

    Exercise Task

    Write a function 'calculateTotal' that takes 'price' and 'quantity' as arguments and returns the total. Log the result for (25, 4).
    script.js
    Console

    Click "Run" to execute your code...

    🍪 We Value Your Privacy

    We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies according to our Privacy Policy.

    Learn More