> For the complete documentation index, see [llms.txt](https://program.hackyourfuture.dk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://program.hackyourfuture.dk/course-content/frontend/advanced-javascript/week2.md).

# Week 2

This session is about how you in Javascript can assign functions to variables, pass them as arguments, and return them from other functions. You'll learn how to use callback functions to handle asynchronous operations, such as fetching data from a server or responding to user events.

By understanding the difference between synchronous and asynchronous code, you'll be able to write more efficient and responsive programs which will make for a much better user experience.

## Contents

* [Preparation](https://program.hackyourfuture.dk/course-content/frontend/advanced-javascript/week2/preparation.md)
* [Session Plan](https://program.hackyourfuture.dk/course-content/frontend/advanced-javascript/week2/session-plan.md) (for mentors)
* [Assignment](https://program.hackyourfuture.dk/course-content/frontend/advanced-javascript/week2/assignment.md)

## Session Learning Goals

By the end of this session, you will be able to:

* [ ] Assign functions to variables
* [ ] Pass functions as arguments
* [ ] Return functions from other functions
* [ ] Call functions inside other functions
* [ ] Distinguish between synchronous and asynchronous code
* [ ] Explain why we need code to run asynchronously sometimes
* [ ] Write and use callback functions
* [ ] Use `setTimeout()` for asynchronous operations
* [ ] Use `addEventListener()`

```js
// Example: Using setTimeout() with a callback function
const callback = () => console.log("Do something later");
setTimeout(callback, 1000);
console.log("Do something now");
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://program.hackyourfuture.dk/course-content/frontend/advanced-javascript/week2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
