Loading Please Wait...
It has become a common practice to declare arrays using const.
const services = ["Website", "Applications", "Software"];
An array declared with const cannot be reassigned.
const services = ["Website", "Applications", "Software"];
services = ["Design", "Marketing"]; // Not Allowed
The keyword const is a little misleading. It does NOT define a constant array. It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.
const services = ["Website", "Applications", "Software"];
// Elements can be changed
services[0] = "Design";
// Add an element
services.push("Website");
// Remove an element
services.pop();
JavaScript const variables must be assigned a value when they are declared. Meaning: An array declared with const must be initialized when it is declared. Using const without initializing the array is a syntax error.
// This is not allowed
const services;
services = ["Website", "Applications", "Software"];
// This is allowed
var services;
services = ["Website", "Applications", "Software"];
An array declared with const has Block Scope. An array declared in a block is not the same as an array declared outside the block.
const services = ["Website", "Applications", "Software"];
// Here services[0] is "Website";
{
const services = ["Design", "Marketing"];
// Here services[0] is "Design";
}
// Here services[0] is "Website";
An array declared with var does not have block scope.
var services = ["Website", "Applications", "Software"];
// Here services[0] is "Website";
{
var services = ["Design", "Marketing"];
// Here services[0] is "Design";
}
// Here services[0] is "Design";
Redeclaring an array declared with var is allowed anywhere in a program.
var services = ["Website", "Applications"]; // Allowed
var services = ["Applications", "Software"]; // Allowed
services = ["Website", "Software"]; // Allowed
Redeclaring or reassigning an array to const, in the same scope, or in the same block, is not allowed.
var services = ["Website", "Applications"]; // Allowed
const services = ["Website", "Applications"]; // Not allowed
{
var services = ["Website", "Applications"]; // Allowed
const services = ["Website", "Applications"]; // Not allowed
}
Redeclaring or reassigning an existing const array, in the same scope, or in the same block, is not allowed
const services = ["Website", "Applications"]; // Allowed
const services = ["Website", "Applications"]; // Not allowed
var services = ["Website", "Applications"]; // Not allowed
services = ["Website", "Applications"]; // Not allowed
{
const services = ["Website", "Applications"]; // Allowed
const services = ["Website", "Applications"]; // Not allowed
var services = ["Website", "Applications"]; // Not allowed
services = ["Website", "Applications"]; // Not allowed
}
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved