> For the complete documentation index, see [llms.txt](https://raffycastlee.gitbook.io/marcyannotes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raffycastlee.gitbook.io/marcyannotes/codechallenge-curriculum/unit-0/20240930.md).

# Lecture: Functions in JS

with Carmen Salas • 2024/09/30

[slides](https://docs.google.com/presentation/d/1QhVm-4XI3AioawndAhNcDFNe_1Qp_cuPtfuVfud5sys/edit#slide=id.g2866fcd9a8b_0_259)

* [Basic Concepts](#basic-concepts)
* [Functions in JavaScript](#functions-in-javascript)
* [Console.log vs Return](#consolelog-vs-return)
* [Parameters vs Arguments](#parameters-vs-arguments)
* [Testing our Code](#testing-our-code)

***

## Basic Concepts

**variables** are containers for values (strings, numbers, booleans, etc.) In ES6 (EcmaScript6), we use either **let** or **const**.

* `let` allows variable re-definition
* `const` doesn't! Although some types/objects like `Arrays` can work around this to an extent.
* `var` keyword is no longer used in modern JS practices but it used to serve both the purposes of `let` and `const` with the key difference being that it is *NOT* block-scoped.

## Functions in JavaScript

A **JavaScript function** is a block of code designed to perform a particular task, and is executed when it is *invoked* or *called*.

Typical function syntax:

```js
   function hello (name) {
      return "hello" + name;
   };
```

* `function` - JS keyword for defining functions
* `hello` - function name
* `name` - function parameter
* `return "hello" + name;` - function body (*anything in between the curly braces is considered as such*)

Arrow function syntax:

```js
   const hello = (name) => {
      return "hello" + name;
   };
```

* no keyword `function` - `hello` - is the variable name, and also serves as the function name
* without a name (in this case, `hello`) and the `const` keyword, the function becomes **anonymous**

## Console.log vs Return

* `console.log` is essentially a *print* function
* `return` is used, within a function body, to terminate it and *optionally* return a value

## Parameters vs Arguments

```js
   const hello = (name) => {
      return "hello" + name;
   };

   hello("Motun");
```

* `name` - function parameter (placeholder for arguments)
* `"Motun"` - function argument (value passed into a function when it is *invoked*)

## Testing our Code

Just `console.log()` it.
