# 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](/course-content/frontend/advanced-javascript/week2/preparation.md)
* [Session Plan](/course-content/frontend/advanced-javascript/week2/session-plan.md) (for mentors)
* [Assignment](/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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
