Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • If no, could you please provide an example of a non-literal array.
  • If yes, could you please provide an example of a variable that has a literal and non-literal variety (assuming such a thing exists) and explain how it differs to an array.
like image 569
Stringers Avatar asked Nov 30 '22 21:11

Stringers


1 Answers

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.

like image 138
melpomene Avatar answered Dec 04 '22 10:12

melpomene