5

I have been messing around with the Google's JavaScript code and I saw in their code that they define array in different way.

What's so unique in writing array like that?

var arr = (['b' , 'f' , 's']);

why is the "()" in that code, it could be fine either like that:

var arr = ['b' , 'f' , 's'];
  1. is it have purpose?

Thank you in advance.

3
  • 1
    Maybe, an artifact of closure compiler, or gwt. Commented Oct 20, 2011 at 23:08
  • 1
    Parens merely ensure that the expression inside them is evaluated before any operation involving that expression. In your case the parens don't make any difference... Commented Oct 20, 2011 at 23:17
  • Is it possible that you are checking the source of the Google Closure library? Commented Oct 20, 2011 at 23:21

2 Answers 2

5

No! It has no purpose whatsoever in the language and can be safely removed. (It might have something to do with coding style or minification.)

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by "in the language"? You mean generally?
@ŠimeVidas: I mean that, for a random example, if the whole site had hash codes of each file and was designed to shut down the server if any had changed... it would matter. But if we're talking about a single piece of JavaScript, nothing more, it makes zero difference. A bit unnecessary, I guess :)
1

It's purely stylistic. The brackets don't change the meaning of the array in any way..

proof

1 Comment

I think you used the wrong word. The brackets [] are required for the array notation. The parentheses () are optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.