Stringers
Stringers

Reputation: 125

Javascript - Are all arrays literal?

I was reading through the Mozilla Javascript documentation and I am a bit confused about the use of the word "literal". Specifically, are all arrays in Javascript considered to be "array literals".

Upvotes: 4

Views: 1245

Answers (6)

Pac0
Pac0

Reputation: 23149

Array literal means an array value written as code, like this : [4, 5, 6]

let array1 = [4,5,6]; // array created with an array litteral
console.log(array1);

An array, is an object with the type Array.

You can create an array by writing an array literal, or by other ways, for instance (as another answer mentioned) by using the Array constructor, or with for instance the split function : "4 5 6".split(' ');

let myString = '4 5 6';
let array2 = myString.split(' ' ); // we get an array from split, but no literal was used
console.log(array2);

the result of the split function is an array as well, but it is not written literally in the code.

Upvotes: 8

Senal
Senal

Reputation: 1610

You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script

As the word explains literal, it is that we literally specifies some value. It is a type of object initializer in JavaScript.

That being said, now to your question : are all arrays in Javascript considered to be "array literals"?

Note : An array literal is a type of object initializer.

To me the question should be are all the arrays in JavaScript is initialized using "array literals"? Because an array literal is not a type but a way of initializing an array.

You could initialize an array in different methods like below:

let x = new Array(4);
console.log(x.length);
let y = new Array('a', 'b');
console.log(y);
let z = [];               // Array literal
console.log(z.length);
let p = ['a', 'b', 6];    // Array literal
console.log(p);

Upvotes: 1

ourlifeinkodaks
ourlifeinkodaks

Reputation: 1

I think it's just the phrasing of sentences that is confusing you. Let me put it in a more understandable way:

var array = [1, 2, 3]**; 

Here [] is the array literal and 1, 2, 3 are the array expressions.

Hope that helps.

Upvotes: 0

Amadan
Amadan

Reputation: 198314

There is no "literal array" and "non-literal array". The distinction is between "array literal" and "array value". A "literal" here is a representation of a value in source code. For example, here is a variable being initialised from a number literal (42):

let a = 42;

Here is a variable being assigned a value that is calculated; there are two literals (11, 31) in this line:

let b = 11 + 31;

In the same way, {} is a literal for the value we can describe as "an empty array". [1, 2, 3] is an array literal. Array(1, 2, 3) will create that same array, but it is not an array literal; it calculates the array by calling a function.

Especially with compiled languages, values of literals can start off already "baked in" into the executable, meaning that no time needs to be spent during execution on allocating and initialising them; they're already present in the data section of the executable. You can see this both in C and in Java, for example.

Upvotes: 0

Willem van der Veen
Willem van der Veen

Reputation: 36620

An array literal is an array created in the following manner:

let arr = [1,2,3];

That's all there is to. You can also create array like this:

let arr = new Array(10);

The function of a array literal is that you easily declare and initialize the array at the same time.

let arr = [];  // declaring;

arr[0] = 1;
arr[1] = 2;
// etc. = initializing

With array literal syntax we can do these steps simultaneously and this allow for more concise code.

There is also an Object literal syntax which is the following:

let obj = {prop1: 1}

this is shorthand for:

let obj = new Object();

obj.prop1 = 1;

Upvotes: 3

melpomene
melpomene

Reputation: 85767

An array is a type of value (it can be stored in variables, passed to functions, etc).

An array literal is a type of syntax. Specifically it refers to [ ... ] (where ... is a comma-separated list of values).

Executing an array literal creates an array at runtime. However, there are other ways to create arrays:

var x = new Array();

x contains an array that was not created from a literal.

Similarly, there are all kinds of operations that produce strings, but only "..." and '...' are string literals.

Upvotes: 8

Related Questions