Question:
JavaScript : Best place to start the statement curly brackets?
jonny s
2012-08-30 03:37:50 UTC
Just starting to learn JavaScript and I'm wondering, what is the more popular or widely accepted way of starting a statement's curly brackets:

function Update () {
}

OR

function Update ()
{
}

I've heard both are equally accepted, but I don't ever believe that kind of answer. Which way do MOST programmers do it?
Six answers:
WK of Angmar
2012-08-30 03:44:04 UTC
Most JavaScript programmers use JavaScript minifiers/compressors after they have finished writing their code to reduce file sizes, and this will reduce the file down and it won't matter which method you use in the end. Therefore, you should go with whatever you feel more comfortable with.



The majority of JavaScripters, however, will use the first way:



function Update() {

}



I personally prefer that way too.
just "JR"
2012-08-30 13:16:42 UTC
It is a question of taste and habits! :-)

Both are valid. Both are working.

HOWEVER:

function foe()

{

}

ALIGNS the brackets vertically, on the same column.

If your indentation is correct, it is very easy to find "start" and "end" of any procedure.

An editor like Notepad++, when located on a bracket ({, [ or ( ), will show a vertical red line linking the brackets together. It makes life MUCH easier!

Look at the number of times people are posting pieces of code that don't work... because they forgot a bracket... somewhere! Why? because they use the form

function foe() {

...

...

}

! LOL!

(If you were to work for me - had 25 programmers - and did NOT align your brackets, you would have received your last pay...)
David
2012-08-30 11:32:15 UTC
Putting the starting curly bracket on the same line as the function name is the correct style because the other way is inconsistent, as it will lead to errors in different circumstances. Consider the following example:



// ...



return {



};



This will return an empty object literal as expected. But this, however, will not work:



// ...



return

{

};



JS's auto semicolon insertion will put a semicolon after "return", terminating the function. The object literal will be interpreted as a block statement.
green meklar
2012-08-30 18:21:15 UTC
I myself strongly recommend the second pattern. Putting both the curly brackets in the same column just makes it easier to read and understand the code. I'm not sure if most programmers do it this way, but I suspect that most programmers reading your code would prefer it if YOU did it that way. As a programmer, certainly I would prefer it if they did it that way.



David's objection is a very weak one and, in my opinion, is not worth sacrificing readability.
Anonymous
2012-08-30 23:10:36 UTC
Hello JR of web2coders.



I'm wondering why you stole $400 from me?

I registered for your course and never hear back from you whenever I attempt to communicate with you.

BEWARE EVERYONE - THIS GUY IS A THIEF AND CAN'T BE TRUSTED!
Matt Anonymous
2012-08-30 11:14:07 UTC
it doesn't matter once you minify things (which should be standard practice for commercial rollout) it removes most if not all whitespace, so basically it's whatever you find easier to read.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'JavaScript : Best place to start the statement curly brackets?' (Questions and Answers)
6
replies
help needed in c program?
started 2007-04-20 06:45:26 UTC
programming & design
Loading...