To put multiple elements in one const in JavaScript, you can use an array or an object.
index.tsx108 chars3 lines
In the above example, we have assigned an array of fruits to a const variable fruits
. The const
keyword ensures that the variable fruits
cannot be reassigned to a different array or value.
index.tsx131 chars3 lines
In the above example, we have assigned an object of a person with three properties to a const variable person
. Similar to the array example, the const
keyword ensures that the variable person
cannot be reassigned to a different object or value.
Note that although the const
keyword prevents the variable from being reassigned, it does not prevent the array or object itself from being modified. To ensure that the array or object is never modified, you can use Object.freeze
method, but that is not recommended in all scenarios.
gistlibby LogSnag