Please explain Curly Braces


#1

I am understanding that the curly braces are telling the computer when to start and stop, or where your line of code ends. What i am confused on is how i see them being used. Sometime they are before the line of code and then after, i get that, then other time i will see 2 curly braces at the end. Can someone break this down for me in laymans terms?

Replace this line with your code.


#2

well, you close a function with a curly bracket:

var myFunction = function(){
  /* do something */
}

Or you if statement:

if (true){
  console.log("yea");
}

but what happens if you nest the if statement inside the function? This happens:

var myFunction = function(){
  if (true){
  console.log("yea");
  } /* this closing brackets closes the if statement */
} /* this closing bracket closes the function */

So yea, now you have two closing curly bracket after each other. You can even 3 (for example: function, for loop, if statement)

It is important to keep track of the curly brackets, to see which curly brackets closes something, and what is nested inside a loop, function or if statement