Friendly Guide to JavaScript Functions: Making Your Code Work for You

JavaScript functions are like little helpers that can perform specific tasks for you. Think of them like robots that can carry out instructions you give them. Just like robots, functions can take inputs, perform actions, and give you back an output.

Let’s start with an example. Imagine you want to create a website that calculates the total cost of a purchase. You could write a function called calculateTotalCost() that takes the number of items, the cost of each item, and any discounts as inputs, and then calculates the total cost as an output.

Here’s an example of how you might write that function:

function calculateTotalCost(items, costPerItem, discount) {
let subtotal = items * costPerItem;
let total = subtotal - discount;
return total;
}

You can call this function by passing in the inputs and assign the output to a variable:

let items = 3;
let costPerItem = 20;
let discount = 10;
let totalCost = calculateTotalCost(items, costPerItem, discount);
console.log(totalCost); // Output: 50

As you can see, the function takes the inputs (3 items at $20 each with a $10 discount), does the calculation (3 * 20–10), and returns the output (50).

JavaScript functions are a powerful tool that can help make your code more efficient and organized. They allow you to separate different logic and tasks into reusable blocks of code, making it easy to perform the same actions multiple times with different inputs. Whether you’re creating a simple calculator or a complex web application, functions are an essential part of any JavaScript programmer’s toolbox.

It’s worth noting that functions are not unique to JavaScript, they are a fundamental concept in many programming languages.
Whether you are working with Java, Python, C#, or any other programming language, you will likely encounter functions and learn how to use them in your code.
The basic concept of functions is the same across different languages, so the knowledge and skills you gain while working with functions in JavaScript can be applied to other languages as well.

Don’t be intimidated by functions, they are not hard to understand and you can start using them right away in your code.
Remember that functions are like robots, they can take inputs, do some work, and give you back an output. Always keep in mind that the goal of a function is to make your code more readable and less repetitive.

Start experimenting with different functions and inputs, and see how they can help you simplify your code.
As you continue to learn and grow as a developer, you’ll find that functions become an invaluable tool in your programming toolbox.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists. ❤️

Feedback Display Feedback Display