Free JavaScript course. Sign Up for tracking progress →

JavaScript: Creating (defining) functions

Defining your own functions makes it much easier to write and maintain programs. Functions allow you to combine several complex operations into one. For example, sending an email on a website is a complex process that involves interaction with external systems (the Internet). Since you can define functions, you can hide all the complexity behind one simple function:

// A hypothetical example
// A place where the function is taken from
import { send } from 'mailer';

const email = 'support@hexlet.io';
const title = 'Help';
const body = "I've written a success story, how can I get a discount?";

// One little call and loads of logic inside
send(email, title, body);

A call like this contains a lot of logic. It connects to the mail server, generates a valid request based on the message header and body, then sends it and closes the connection.

Let's create our first function. Its job is to print out a greeting:

Hello, Hexlet!
// Defining the functions
// The definition doesn't call or execute a function
// We're just declaring this function exists now
const showGreeting = () => {
  // Use 2-space indents inside the function body for readability
  const text = 'Hello, Hexlet!';
  console.log(text);
}

// Calling a function
showGreeting(); // => "Hello, Hexlet!"

https://replit.com/@hexlet/js-basics-functions-define

Unlike normal data, functions perform actions, so their names almost always have to be verbs: "build something", "draw something", "open something", etc.

Anything described inside the curly brackets {} is called a function body. It can contain any code. Consider it a small independent program, a set of arbitrary statements. The body is executed precisely when the function is called. And each function call executes the body independently of other calls. By the way, the body can be empty:

// A minimal function definition
const noop = () => {
// There could be some code, but there isn't any
}

noop();

Defining a function looks suspiciously like creating a constant. Indeed, a function definition consists of two parts: the definition itself and its assignment to a constant:

  1. Definition: () => { }
  2. Assignment: const nameOfFunction = ...

It is technically possible to create a function that is defined without a name and therefore unable to be called:

() => {
  // Valid but useless code
};

The notion of "creating a function" has many synonyms, such as "implement" or "define". All of them are encountered in everyday practice.

Instructions

Implement the printMotto() function, which prints the phrase Winter is coming.

printMotto(); // => "Winter is coming"

In tasks where you have to implement a function, you don't need to call it. Automated tests will call this function to check if it works. The example call above is shown just to give you an idea of how your function will be used.

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.

Tips


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