# Use of curly braces in functions and if statements

**URL:** <https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 26, 2020, 7:21pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729 "2020-02-26T19:21:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzimba](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jzimba/32/134914_2.png) [@jzimba](https://forum.freecodecamp.org/u/jzimba)\
**Post date:** [February 26, 2020, 7:21pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/1 "2020-02-26T19:21:26Z")

</div>

I need help understanding when to use curly braces in an if statement. I was attempting to solve the Basic Algorithm Scripting: Where do I Belong challenge. Compare the 2 functions below:

```auto
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  let sortedArr = arr.sort();
 for (var a = 0; a < arr.length; a++) {
    if (arr[a] >= num) return a;
  }

  return arr.length;
}

```

and

```auto
function checkIndex(u,n){
  let papa = u.sort();
  for (let i = 0; i < u.length; i++){
    if (sortedArr[i] >= num){
      return i;
  }else{
    return u.length
    }
}
}

console.log(checkIndex([], 1)); //prints undefined
console.log(getIndexToIns([], 1)); //prints out 0. The only difference is the curly braces.

```

---

<div class="post-metadata">

**Author:** ![clanmaster](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clanmaster/32/111859_2.png) [@clanmaster](https://forum.freecodecamp.org/u/clanmaster)\
**Post date:** [February 26, 2020, 7:29pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/2 "2020-02-26T19:29:02Z")

</div>

syntax is like this:  
const functionName(){  
block code;  
}  
for conditionals:  
if(boolean expression){  
block code;  
}

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [February 26, 2020, 7:32pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/4 "2020-02-26T19:32:56Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

**Note:** Backticks are not single quotes.

![markdown_Forums](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![jzimba](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jzimba/32/134914_2.png) [@jzimba](https://forum.freecodecamp.org/u/jzimba)\
**Post date:** [February 26, 2020, 7:33pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/5 "2020-02-26T19:33:43Z")

</div>

Thanks for that. The solution provided however, does not include the curly braces after the if statement. Do you know how the curly braces affect the code?

---

<div class="post-metadata">

**Author:** ![clanmaster](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clanmaster/32/111859_2.png) [@clanmaster](https://forum.freecodecamp.org/u/clanmaster)\
**Post date:** [February 26, 2020, 7:36pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/7 "2020-02-26T19:36:39Z")

</div>

Ahhhhh thanks … check that ;o)

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [February 26, 2020, 7:37pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/8 "2020-02-26T19:37:31Z")

</div>

If a block consists of a single line, you can choose to omit the curly braces. However, you will very often find that people prefer that you use curly braces for all `if` and `else` blocks for consistency and readability.

---

<div class="post-metadata">

**Author:** ![chuckadams](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chuckadams/32/109651_2.png) [@chuckadams](https://forum.freecodecamp.org/u/chuckadams)\
**Post date:** [February 26, 2020, 8:08pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/10 "2020-02-26T20:08:24Z")

</div>

I personally do like the one-line `if (foo) return bar;` in my own personal code, but only for returns or throws. And in a team I stick to always using the curlies. It’s just too likely that someone will eventually evolve it into a multi-line statement and screw things up.

---

<div class="post-metadata">

**Author:** ![jzimba](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jzimba/32/134914_2.png) [@jzimba](https://forum.freecodecamp.org/u/jzimba)\
**Post date:** [February 26, 2020, 8:21pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/11 "2020-02-26T20:21:20Z")

</div>

Ahh thanks. That makes sense.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 1:01pm UTC](https://forum.freecodecamp.org/t/use-of-curly-braces-in-functions-and-if-statements/353729/12 "2021-06-01T13:01:39Z")

</div>


