# Javascript - confused about });

**URL:** <https://forum.freecodecamp.org/t/javascript-confused-about/56863>\
**Category:** General\
**Created:** [November 19, 2016, 12:13am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863 "2016-11-19T00:13:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiepyoung](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jamiepyoung](https://forum.freecodecamp.org/u/jamiepyoung)\
**Post date:** [November 19, 2016, 12:13am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/1 "2016-11-19T00:13:28Z")

</div>

I seem to be getting in a muddle with when to use }); in javascript.

I keep getting errors and just adding more }'s to try and clear them.

I know they enclose functions and if/else arguments etc. Why am I finding this so much of a problem - is there a simple guide i’m missing??

Thanks

---

<div class="post-metadata">

**Author:** ![michealhall](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@michealhall](https://forum.freecodecamp.org/u/michealhall)\
**Post date:** [November 19, 2016, 12:36am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/2 "2016-11-19T00:36:12Z")

</div>

They need to match … an opening { with a closing }. Same with ( and ).

That’s it really.

You most commonly see }) when closing a callback function inside a call to another function like:

```auto
const some_array = [];

some_array.forEach(function( elem, e ){
// DO STUFF WITH ELEMENT
});
```

There you hare calling the forEach method on the array, which takes as an argument a function … but your defining your function right in place, so you see a `function(){}` block because a standard javascript function needs the braces.

Really, it’s …

```auto
some_array.forEach( callback );
```

And the forEach method executes the callback for each element in the array.

You could easily define the callback first and pass it to the forEach method.

```auto

function callback( elem, e ){
// DO SOMETHING WITH ELEM
}

some_array.forEach( callback );

```

If that made things clearer to read.

Curly braces are used to surround blocks of code … statements in a function, a while block, an if block, statements in a for … loop, etc.

[MDN Reference on code blocks.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block)

They are also used for the creation of objects, called an object literal. So you might see …

```auto
const some_obj = {
   'property_name': property_value
};
```

[MDN Reference on Object creation.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Creating_objects)

Most code editors I know of offer opening/closing bracket highlights to help you spot the beginning and end of a bracket and that can be helpful in highlighting mismatches (where you’ve opened a block, but not closed it).

Regards,  
Micheal

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 19, 2016, 12:40am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/3 "2016-11-19T00:40:41Z")

</div>

> [@jamiepyoung](#):
>
> is there a simple guide i’m missing

Yes, you need to close everything. 🙂

If you don’t use a code editor / IDE, or your code editor doesn’t close your parentheses, braces and brackets, you can do a simple trick to not to forget it. When you write an opening bracket, immediately close it and just get back using your arrow key.

---

<div class="post-metadata">

**Author:** ![michealhall](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@michealhall](https://forum.freecodecamp.org/u/michealhall)\
**Post date:** [November 19, 2016, 12:50am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/4 "2016-11-19T00:50:52Z")

</div>

That’s exactly what I forgot to add … 🙂

I always write out the boilerplate first … that way I don’t forget.

Good tip!

---

<div class="post-metadata">

**Author:** ![gtrabbit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gtrabbit/32/21788_2.png) [@gtrabbit](https://forum.freecodecamp.org/u/gtrabbit)\
**Post date:** [November 19, 2016, 1:08am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/5 "2016-11-19T01:08:58Z")

</div>

if you’re using codepen, and you have your cursor on a bracket, you’ll notice that it gives the bracket a little underline. The “matching” bracket will also be underlined. You may find that the matching bracket is not the one you thought it was.

---

<div class="post-metadata">

**Author:** ![jamiepyoung](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jamiepyoung](https://forum.freecodecamp.org/u/jamiepyoung)\
**Post date:** [November 19, 2016, 8:08am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/6 "2016-11-19T08:08:01Z")

</div>

Thanks everyone. I was over-complicating things I think.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 6:09am UTC](https://forum.freecodecamp.org/t/javascript-confused-about/56863/7 "2021-01-16T06:09:34Z")

</div>

Closed on January 16, 2021, 6:09am UTC


