# Week 2

## Learning goals

* [ ] Conditions: if, elseif, else. Negated.
* [ ] [Functions](#functions): [Calling](#calling-a-function-on-something), defining, [parameters, arguments](#parameters-and-arguments)
* [ ] Global vs local scope
* [ ] For loop

Teaching note. Start off explaining functions with how to use a function e.g. explain why the Math.random function is smart, or Math.max.

## Relevant links

* [Preparation](/course-content/foundation/intro-to-javascript/week2/preparation.md)
* [Assignment](/course-content/foundation/intro-to-javascript/week2/assignment.md)
* [Session Plan](/course-content/foundation/intro-to-javascript/week2/session-plan.md)

### Recap Logical operators

```js
0 = false
1 = true
```

#### AND `&&`

| `&&` | 0 | 1 |
| ---- | - | - |
| 0    | 0 | 0 |
| 1    | 0 | 1 |

#### OR `||`

| `\|\|` | 0 | 1 |
| ------ | - | - |
| 0      | 0 | 1 |
| 1      | 1 | 1 |

So you can say that false in combination with `&&` always returns false

```js
true && false; //-> false
false && true; //-> false
false || true; //-> true
true || false; //-> true
```

### Typeof

`typeof` always returns the data type in a string.

So for example:

```js
let bar = 42;
typeof bar; //-> 'number'
typeof typeof bar; //->  'string'
```

So the data type of what `typeof` returns is always a string, bar on the other hand is still a number.

## Functions

A function is a reusable piece of code. It is used to hide away abstraction! Functions are *very* important in JavaScript, to the extent that some people call JavaScript a "function-oriented" language.

### Two sides of a function

There are two sides to using function, using a function (calling a function) and creating a function.

#### Function usage

When you call a function some javascript code is run. Here's a simple example:

```js
const randomNumber = Math.random();
console.log(randomNumber); // logs out some number between 0 and 1
```

Here `Math.random` is a function. To activate the function we call it using parentheses `()`. When calling it we get a randomNumber! We now don't need to think about all the code it takes to create a random number in javascript, we simply call the function and get a random number. Code has been abstracted away for us!

Some functions is called with arguments, for example:

```js
const maxNumber = Math.max(3, 5); // 3 and 5 are arguments
console.log(maxNumber); // logs out 5
```

Arguments are used to control the code inside a function.

#### Function creation

When we create a function we use the function keyword:

```js
let todoItem = "Buy Groceries";
console.log(todoItem);

// Create the function
function checkItem() {
  todoItem = todoItem + " - done";
}

// use the function by calling it using ()
// If we don't call the function that code will NEVER EVER IN A MILLION YEARS run!
checkItem();
console.log(todoItem);
```

#### Parameters and arguments

```js
// a and b are called parameters. This function has two parameters
function sum(a, b) {
  return a + b;
}
```

```js
// 5 and 10 are called arguments now when called in a function
// Arguments are "passed" to the function: "we pass `4` and `5` to the function sum
const returnedSum = sum(5, 10);
console.log(returnedSum); // logs 15
```

So remember the difference between the word "parameter" and "argument". Many people confuse them, and that's not a big problem, but understanding the difference is always nice:

* A parameter is the name you want to give to the variable that is available inside of the function.
* An argument is the actual value you want to assign to the parameters when you call the function.

Parameters acts as a placeholder for the arguments. The value of the parameter will get substituted with the value of the argument.

```js
function multiply(a, b) {
  // the value of a will be substituted with the value of 10!
  // the value of b will be substituted with the value of 4!
  console.log(a, b); // logs out 10 4
  return a * b;
}

multiply(10 * 4);
```

**Return value**

Sometimes we want to get a value back when calling a function. e.g. in the sum example. We want to call the function and get the sum back!

```js
function sum(a, b) {
  return a + b;
}
const returnedSum = sum(5, 10); // the variable returnedSum captures the return value from calling the function!
console.log(returnedSum); // logs 15

// If we don't return, we cannot capture the returned value!
function sumNoReturn(a, b) {
  a + b; // no return!
}
const returnedSum = sum(5, 10);
console.log(returnedSum); // logs undefined
```

If we don't return anything from the function, it will automatically return `undefined`. This is the functions way of saying that nothing was returned.

### Calling a function on something

In JavaScript, you can call functions *on* something. By this, we mean that you use the dot to call the function. For instance, when we say "call method `trim` on string `s`", we mean:

```js
let s = " this is a string  ";
s.trim(); // -> "this is a string"
```

However, there are functions that you don't call on anything:

```js
function sum(a, b) {
  return a + b;
}
sum(4, 5); // -> 9
```

Here, you call the function `sum` on nothing.


---

# 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/foundation/intro-to-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.
