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 Const

JavaScript is a popular programming language used for creating interactive web pages. It is a versatile language that allows developers to create dynamic and responsive web applications. One of the key features of JavaScript is the ability to declare variables. In JavaScript, there are three ways to declare variables: var, let, and const. In this article, we will focus on the const keyword and its usage in JavaScript.

Brief Explanation of JS Const

The const keyword is used to declare a constant variable in JavaScript. A constant variable is a variable whose value cannot be changed once it has been assigned. This means that once a value is assigned to a constant variable, it cannot be reassigned or modified. The const keyword is used to declare variables that are meant to be constant throughout the program. This is useful when you want to declare a value that should not be changed, such as a mathematical constant or a configuration value.

When you declare a constant variable using the const keyword, you must assign a value to it at the time of declaration. This value cannot be changed later. If you try to reassign a value to a constant variable, you will get an error. This makes it easy to catch errors in your code and ensures that your program is more reliable.

Code Examples

Let's take a look at some code examples to see how the const keyword is used in JavaScript:

<script>
  const PI = 3.14159;
  console.log(PI); // Output: 3.14159

  PI = 3.14; // Error: Assignment to constant variable.
</script>

In this example, we declare a constant variable PI and assign it the value of 3.14159. We then try to reassign a new value to PI, which results in an error because PI is a constant variable and its value cannot be changed.

<script>
  const CONFIG = {
    apiUrl: 'https://api.example.com',
    apiKey: '1234567890'
  };

  console.log(CONFIG.apiUrl); // Output: https://api.example.com

  CONFIG.apiUrl = 'https://api.newexample.com';

  console.log(CONFIG.apiUrl); // Output: https://api.newexample.com
</script>

In this example, we declare a constant variable CONFIG and assign it an object with two properties: apiUrl and apiKey. We then log the value of CONFIG.apiUrl to the console, which outputs https://api.example.com. We then try to change the value of CONFIG.apiUrl to https://api.newexample.com, which is allowed because we are not reassigning the entire object, only modifying one of its properties.

Conclusion

The const keyword is a useful feature in JavaScript that allows developers to declare constant variables that cannot be changed once they have been assigned. This makes it easier to catch errors in your code and ensures that your program is more reliable. By using the const keyword, you can declare values that should not be changed, such as mathematical constants or configuration values.

References

Activity