Decoding Tech
Published in

Decoding Tech

Functions & Variables

We will be putting our knowledge about data into practice & learn how to create and use variables and functions. And Lena will be helping us along the way.

Remember Lena from the last article? No? Let’s quickly recap:

const name = "Lena";
const profession = "Back-end Developer";
const age = 31;
const monthlySalary = 8534.50;

The above lines of code are storing information about our friend Lena so that we can use them later on in our code. All of the variables above are stored with the keyword const which means their values won’t change throughout the code. This is certainly true for some of these variables, e.g. her name and profession, and her current age, but depending on the tool we are building, variables can also change in your code and be re-assigned, e.g. the monthlySalary.

Down below we will be writing a little program that calculates the amount of money on Lena’s savings account where we will assume her monthly salary increases over time linearly (a rough assumption, of course!). Scroll to the coding exercise to see how we did that!

Make let — not var

In JavaScript, there are three keywords that you can use to create (declare) a variable: var, const and let. The keyword var has been there from the beginning whereas const and let have been introduced in 2015 with a major update to the language called ECMAScript 2015 — ES6 (oh yes, languages evolve over time! Usually, they get better 😎).

What are the differences between the three? Let’s start with the oldie: var is used to declare variables in the function scope (see below) and can be re-assigned and overwritten within that scope as you wish. Often, however, that’s not really what you want 🙈

The two youngster keywords are used within block scope and the difference between const and let is relatively simple: if your variable is a constant and should not be reassigned, use const. If your variable changes and needs to be re-assigned, use let.

It’s all about the scope

The first time you assign a value to a new variable is called initialization. If you try to use a variable somewhere in your code but the variable has not been initialized yet OR has been declared in a different scope, then your compiler will complain and tell you that your variable is undefined.

Scopes in your code define areas in which variables are accessible. We distinguish between a global scope (= variable is accessible from everywhere) and a local scope (= variable is accessible within this scope but not outside).

Within the local scope, there are two subcategories: block and function scope. Block scope is defined by an if or switch statement (conditional), or a for or while loop (iterations). Function scope defines the scope of a function and as mentioned above, to make use of this scope, we can declare variables with the keyword var.

What is a function?

But what is a function? A function is a block of code that is designed to perform a specific task that is typically repeated. Functions can have inputs and outputs, so-called return values. However, note that functions do not need to have input parameters, and not every function returns something.

Functions can be called from other parts of your code (invocation) and they help you to build your program more modular. In theory, you can write your code line by line, but then it becomes unreadable and you lose the overview. A function encapsulates a certain part of your code into a separate piece or module — to make your code more readable!

Example: The function sum could have the task to sum-up two or more input values. Whenever we need a sum of some values in our code, we call the function sum and let it do the job 💪

CODING EXERCISE

Now that we know how about variables and functions, let’s do an easy coding exercise putting our knowledge into practice, just for fun 😁 let’s make the following assumptions:

  • Lena wants to put 7% of her monthly salary aside for a savings account
  • Every year, Lena is expected to get a salary raise of 5%

How much money will she have in her savings account at the age of 40?

First, we need a function to calculate the increased salary every year:

function increaseSalary(salary, increase) {
return salary + increase * salary;
}

Now we use let and assign a new variable where we can save the intermediate steps, initialized with the current monthly salary (from above), as well as a variable for the amount of money on the savings account that starts at 0:

let newMonthlySalary = monthlySalary;
let savingsAccount = 0;

Then we are iterating over the 9 years (from 31 to 40) and add 7% of her annual salary (12 months) to the savings account at the end of each year. Also, her salary increases always at the end of the year by 5%:

for (year = age; year < 40; year++) {
savingsAccount += 12 * newMonthlySalary * 0.07;
newMonthlySalary = increaseSalary(newMonthlySalary, 0.05);
}
console.log(savingsAccount);

If we run this simple calculation we find that at the age of 40, Lena can expect to have 79'049.22 CHF in her savings account. We can also find out that her monthly salary is by now at 13'239.81 CHF.

We hope you had fun with this little exercise & maybe it also made you think a bit about salaries & savings 😎. As in the last article, you can test this example easily by simply copy-pasting the code snippets into your browser console (F12). You can of course also input your own values instead of Lena’s 😏

We are looking forward to next week’s topic about algorithms. 💥

Resources:

Join us!
We are a diverse & inclusive tech community with a common goal — to empower women across industries & inspire them through learning and networking opportunities. Join us in our mission for gender equality in the tech industry in Switzerland.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store