JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Array Const

JavaScript is a popular programming language that is used to create interactive web pages. One of the most important features of JavaScript is its ability to work with arrays. Arrays are a collection of values that can be stored in a single variable. In JavaScript, arrays can be created using the const keyword.

The const keyword is used to declare a constant variable. A constant variable is a variable whose value cannot be changed once it has been assigned. This makes it ideal for creating arrays, as the size and contents of an array are typically fixed.

Here is an example of how to create an array using the const keyword:

<script>
  const myArray = [1, 2, 3, 4, 5];
</script>

In this example, we have created an array called myArray that contains the values 1, 2, 3, 4, and 5. Because we used the const keyword to declare the variable, we cannot change the values of the array later in our code.

However, we can still access and manipulate the values of the array using various JavaScript array methods. Here are some examples:

<script>
  const myArray = [1, 2, 3, 4, 5];

  // Accessing values in the array
  console.log(myArray[0]); // Output: 1
  console.log(myArray[2]); // Output: 3

  // Changing the value of an element in the array
  myArray[1] = 10;
  console.log(myArray); // Output: [1, 10, 3, 4, 5]

  // Adding elements to the array
  myArray.push(6);
  console.log(myArray); // Output: [1, 10, 3, 4, 5, 6]

  // Removing elements from the array
  myArray.pop();
  console.log(myArray); // Output: [1, 10, 3, 4, 5]
</script>

As you can see, even though we declared the myArray variable using the const keyword, we can still access and manipulate the values of the array using various JavaScript array methods.

One thing to keep in mind when using the const keyword to create arrays is that the contents of the array can still be modified. This means that if we have an array of objects, for example, we can still modify the properties of those objects even if the array itself is declared as a constant.

Here is an example:

<script>
  const myArray = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 },
    { name: 'Bob', age: 40 }
  ];

  // Modifying the properties of an object in the array
  myArray[1].age = 30;
  console.log(myArray); // Output: [{ name: 'John', age: 30 }, { name: 'Jane', age: 30 }, { name: 'Bob', age: 40 }]
</script>

In this example, we have an array of objects that represent people. Even though we declared the myArray variable using the const keyword, we can still modify the properties of the objects in the array.

Overall, the const keyword is a useful tool for creating arrays in JavaScript. It allows us to create variables that cannot be changed once they have been assigned, which is ideal for creating arrays whose size and contents are fixed.

References

Activity