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'];
Thank you in advance.
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.
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.
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 .
{} 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.
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.)
It's purely stylistic. The brackets don't change the meaning of the array in any way..
proof
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With