# Syntax for declaring functions

**URL:** <https://group.classcentral.com/t/syntax-for-declaring-functions/35780>\
**Category:** JavaScript Bootcamp\
**Created:** [September 21, 2022, 1:22pm UTC](https://group.classcentral.com/t/syntax-for-declaring-functions/35780 "2022-09-21T13:22:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenr2945563](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenr2945563](https://group.classcentral.com/u/stevenr2945563)\
**Post date:** [September 21, 2022, 1:22pm UTC](https://group.classcentral.com/t/syntax-for-declaring-functions/35780/1 "2022-09-21T13:22:41Z")

</div>

In much of the bootcamp, functions have been declared using this syntax:

```auto
function checkScope() {
blah blah blah
}

```

But when discussing arrow functions, there is another syntax presented:

```auto
var magic = function() {
  blah blah blah
};

```

The second uses the declaration keywords `let`, `var`, or `const`, and the first does not. Is there a substantial difference here? Is one syntax preferred over the other?

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 21, 2022, 9:18pm UTC](https://group.classcentral.com/t/syntax-for-declaring-functions/35780/2 "2022-09-21T21:18:34Z")

</div>

Oof… another complex question! (Love them!)

So first of all, I would strongly advise dropping use of the ‘var’ for variable declaration. You will come across them in older code and examples, but for newer code going forward, you should use let/const and ignore var completely.

So the function in this block

```auto
var magic = function() {
  blah blah blah
};

```

is a **function expression** , the format above is called an **anonymous function** (as the function itself has no name) . Note that it is possible to assign a name to the function in a function expression too, which helps during debugging. Please refer to the section here:

> **[Functions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#function_expressions)**
>
> Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some...

You will encounter anonymous functions very often, either in the above form, or as a lambda function (which were introduced in ESS6). They are widely used in functional programming, and patterns that were popularized by React. A lot of JS functions can accept a function as argument, and you will see often an anonymous function used as drop-in. That kind of use is explained here:

[![](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/2X/6/6b6513af9996a12b170413c9bdc1e554097d4b45.jpeg "JavaScript Essentials: Anonymous Functions") ](https://www.youtube.com/watch?v=Y8dztLmscwc)

Lastly, a decent post with naming and examples may help your understanding further:

> **[JavaScript Anonymous Functions](https://www.javascripttutorial.net/javascript-anonymous-functions/)**
>
> In this tutorial, you will learn about JavaScript anonymous functions that can be used as arguments for other functions.

I hope this helps?

---

<div class="post-metadata">

**Author:** ![stevenr2945563](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenr2945563](https://group.classcentral.com/u/stevenr2945563)\
**Post date:** [September 22, 2022, 5:32pm UTC](https://group.classcentral.com/t/syntax-for-declaring-functions/35780/3 "2022-09-22T17:32:24Z")

</div>

Again, super, super helpful. I think what confused me about these being “anonymous” functions is that they are still being stored in a variable, so that variable still felt like the function’s name. Now I see that there are contexts where naming the function by storing it in a variable is not necessary. This video also helped me understand a clear context for when you might pass a function into another function as an argument, an idea that made my head spin when I first read your post.

In the bootcamp, it seems that these anonymous functions are always being called with arguments, and so calling an anonymous function looks exactly like calling a named function, i.e. `add(3, 4);`, if one were to call the last function declared in the tutorial you linked. But now I see that if a function doesn’t need an argument, then a function call looks indistinguishable from any other variable (since it doesn’t need parentheses for its empty argument).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/1X/36206832181ed26910a18e33d8bfef087cf661b1.svg) [@system](https://group.classcentral.com/u/system)\
**Post date:** [November 6, 2022, 5:32pm UTC](https://group.classcentral.com/t/syntax-for-declaring-functions/35780/4 "2022-11-06T17:32:48Z")

</div>

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.
