In javascript there are two ways to create arrays and objects , they can be created by object literals:
new Array()
- object - arrangementnew Object()
- object - object
or they can be created from their primitive objects which are:
[]
- object - arrangement{}
- object - object
What is the difference between using using the literal object and using the primitive object?
I understand that for the
new Array()
, the constructor can receive a parameter to define the propertylength
of the created array, but is this the only difference?
Collecting differences I have found:
Speed. The execution of literals in javaScript is almost twice as fast. You can follow the following operation tokens that exemplify the different steps that the compiler executes when performing one operation or another.
Being:
ARRAY_INIT -> A simple initialization
IDENTIFIER -> The compiler's identification of the Array
CALL object -> The constructor call
() -> Constructor elements, which produce different outputs for one and the other.
The literal generates a "runtime array" while Array() generates an object with functions that give array access, this causes certain limitations when accessing methods.
In short, use literals whenever you can, as it has many advantages over the object.
I understand that both array and string inherit from the Object class in Javascript.
In the first instance I would tell you that String is used for text fields in HTML, to which styles can be applied for Text fields and functions for String.
And array, in my opinion, are used for more complex data, such as nesting one or more html elements (object type), even managing a tree of objects. This is very useful when we work with grids for example.
I leave you more info with the different methods and properties, I hope you find it useful!
String (Object) Javascript: https://msdn.microsoft.com/es-es/library/ecczf11c(v=vs.94).aspx
Array (Object) Javascript: https://msdn.microsoft.com/es-es/library/k4h76zbx(v=vs.94).aspx