Free JavaScript course. Sign Up for tracking progress →

JavaScript: Calling functions

Addition, concatenation, finding the remainder, and the other operations discussed are all basic programming language features. Math is not limited to arithmetic, there are many other domains with their own operations, e.g., geometry. The same goes for strings: you can flip them, change a letter's case, delete extra characters – and that's just the tip of the iceberg. And finally, at a higher level, there is the applied logic of a particular program. Programs withdraw money, calculate taxes, and generate reports. The number of these jobs is endless and different for each program. And they all have to be somehow expressed in code.

The notion of a function expresses any arbitrary operation in programming. Functions can be both built-in and manually written by a programmer. We are already familiar with one built-in function, log(), when we call console.log().

Functions are fundamental building blocks in programming, and it is impossible to accomplish anything without them. We need to get acquainted with them as soon as possible because future courses will deal almost exclusively with functions. First, we'll learn how to use the functions we have already defined, and we'll also learn to define our own functions.

We will start with basic functions that handle strings. Below is an example of the length() function being called. This counts the number of characters in a string:

// length is a function
import { length } from 'hexlet-basics/string';

// length function call with 'Hello!' argument
const result = length('Hello!');
console.log(result); // => 6

A lyrical digression. The first line in this code is an imported function from another module. You will learn about importing and modules on Hexlet, and here they will be "as is" because we need to import to use functions defined in other files. Don't bother if you don't understand the meaning of this step, you can learn more about it in our course Programming fundamentals.

Parameters (or arguments) represent the data the function receives when you call it. This data is what the function uses to compute something and return a result.

We have defined a result constant and told the interpreter to assign it a result returned by the length() function call. In this sense, functions are like operations – they always return the result of their job.

// Calling length() returns the result (the string length)
// which is written in a constant named result
const result = length('Hello!');

Writing length('Hello!') means that we call the function named length and it will take the parameter 'Hello!'. The function length() counts the length of the string passed to it.

The function being called is always indicated by parentheses () following the function name. There can be any number of parameters in parentheses, even nothing can be a parameter. The number of parameters depends on the function used. Consider the function pow() as an example. This raises a given number to a given power. It takes two parameters as input and raises the number passed in the first parameter to the power passed in the second parameter.

import { pow } from 'hexlet-basics/math';

// Calling pow(2, 3) returns the value of 2 to the power of 3
const result = pow(2, 3); // 2 * 2 * 2
console.log(result); // => 8

Broadly speaking, operators and functions are the same things. The only key difference is how they are written. If you think of addition as a function, it might look like this:

// Regular addition
3 + 5; // 8
// Addition represented as a function
// It looks a bit strange, but it conveys the meaning of functions
+(3, 5);


Functions are called. They also return a result that may be used in further calculations or, for example, can be printed.

Self-check. How can you find out what calling console.log() will return? Test it.


The program code features two constants containing company names. Calculate their total character length and print it.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

If you got stuck and don't know what to do, you can ask a question in our community