Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: define array in different way

Tags:

javascript

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.

like image 861
homerun Avatar asked Oct 20 '11 23:10

homerun


People also ask

What are the different ways to create an array in JavaScript?

This example shows three ways to create new array: first using array literal notation, then using the Array() constructor, and finally using String.prototype.split() to build the array from a string.

What is the correct way to define an array in JavaScript?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

Can arrays in JavaScript have different types?

JavaScript arrays can indeed contain any and all types of data. An array may contain other objects (including other arrays) as well as any number of primitive values like strings, null , and undefined .

What is {} and [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.


2 Answers

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.)

like image 142
Ry- Avatar answered Oct 01 '22 12:10

Ry-


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

proof

like image 24
Ben Rowe Avatar answered Oct 01 '22 11:10

Ben Rowe