Skip to content

Instantly share code, notes, and snippets.

@jamiejohnsonkc
Last active June 18, 2020 15:54
Show Gist options
  • Select an option

  • Save jamiejohnsonkc/a53412b19595882e43da0d92d44dbedd to your computer and use it in GitHub Desktop.

Select an option

Save jamiejohnsonkc/a53412b19595882e43da0d92d44dbedd to your computer and use it in GitHub Desktop.
javascript braces, brackets and curlies

From stack:

https://stackoverflow.com/questions/23225375/when-to-use-parentheses-brackets-and-curly-braces-in-javascript-and-jquery

Unfortunately, the best answer is "use them each as appropriate where necessary".

Parenthesis () in JavaScript are used for function calls, to surround conditional statements, or for grouping to enforce Order of Operations.

function myFunc() {
  if (condition1) {

  }
  if ( (1 + 2) * 3) {
    // very different from (1 + 2 * 3)
  }
}

Braces {} are used during the declaration of Object Literals, or to enclose blocks of code (function definitions, conditional blocks, loops, etc).

var objLit = {
  a: "I am an Object Literal."
};

Brackets [] are typically mostly used for accessing the properties of an Object (or the elements of an Array), so mylist[3] fetches the fourth element in the Array.

var mylist = [1,2,3,4];
alert(mylist[2]);```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment