JavaScript const Keyword

The const keyword in JavaScript is used to declare a constant, a value that cannot be changed or redeclared. This tutorial will guide you through the use of const in JavaScript, its scope, and how it differs from other variable declaration keywords like var and let.

JavaScript const: Learn about its declaration, scope, immutability, usage, and best practices with examples and FAQs.

Introduction to const

In JavaScript, const is a keyword used to declare a constant variable. A constant is a value that cannot be changed or redeclared once it has been assigned. Here’s a simple example:

const PI = 3.14159;
console.log(PI); // Outputs: 3.14159
JavaScript

const vs var

The var keyword in JavaScript declares a variable that can be changed or redeclared. On the other hand, const declares a constant that cannot be changed or redeclared. Here’s an example to illustrate the difference:

var x = 10;
x = 20; // This is allowed

const y = 10;
y = 20; // This will throw an error
JavaScript

const vs let

Like const, let is another keyword used to declare variables in JavaScript. The difference is that let allows you to change the value of the variable, but const does not. Here’s an example:

let a = 10;
a = 20; // This is allowed

const b = 10;
b = 20; // This will throw an error
JavaScript

Declaring const Variables

When declaring a const variable, you must assign a value to it. If you don’t, JavaScript will throw an error. Here’s an example:

const PI; // This will throw an error
JavaScript

Scope of const

The const keyword has block scope, meaning it is only accessible within the block of code where it is declared. Here’s an example:

{
  const PI = 3.14159;
}
console.log(PI); // This will throw an error
JavaScript

Using const with Objects and Arrays

When const is used with objects or arrays, the contents of the object or array can be modified, but the variable itself cannot be reassigned. Here’s an example:

const obj = { name: 'John' };
obj.name = 'Jane'; // This is allowed
obj = { name: 'Jane' }; // This will throw an error

const arr = [1, 2, 3];
arr.push(4); // This is allowed
arr = [1, 2, 3, 4]; // This will throw an error
JavaScript

Best Practices

When using const, it’s a good practice to use uppercase letters for the variable name if the value is a primitive and will not change. This makes it clear that the value is intended to be a constant. For example:

const PI = 3.14159;
JavaScript

However, when using const with objects or arrays, it’s common to use lowercase or camelCase, as the contents of the object or array may change:

const myObject = { name: 'John' };
const myArray = [1, 2, 3];
console.log(myObject);
console.log(myArray);
JavaScript

Conclusion

The const keyword in JavaScript is a powerful tool for declaring variables that should not be changed or redeclared. It’s important to understand how const works, its scope, and how it differs from var and let. By using const appropriately, you can write more predictable and less error-prone code.

Remember, while const can help prevent many programming errors, it’s not a silver bullet. Always consider the specific needs of your code when deciding which keyword to use for variable declaration.

Frequently Asked Questions (FAQ)

  • What is the const keyword in JavaScript?

    The const keyword in JavaScript is used to declare a variable that cannot be reassigned or redeclared.

  • What is the difference between const and var?

    The var keyword declares a variable that can be reassigned or redeclared, while const declares a variable that cannot be reassigned or redeclared.

  • What is the difference between const and let?

    The let keyword declares a variable that can be reassigned but not redeclared, while const declares a variable that cannot be reassigned or redeclared.

  • Can I declare a const variable without assigning a value?

    No, a const variable must be assigned a value when it is declared.

  • Can I use const with objects and arrays?

    Yes, you can use const with objects and arrays. The variable itself cannot be reassigned, but the contents of the object or array can be modified.

  • What is the scope of a const variable?

    A const variable has block scope, meaning it is only accessible within the block of code where it is declared.

  • Can I use const in a for loop?

    Yes, you can use const in a for loop, but it will behave differently than var or let. Each iteration of the loop will create a new scope, and the const variable will be redeclared in each iteration.

  • What happens if I try to reassign a const variable?

    If you try to reassign a const variable, JavaScript will throw an error.

  • Is const supported in all browsers?

    const is supported in all modern browsers, but not in Internet Explorer 10 or earlier.

  • Should I always use const instead of var or let?

    Not necessarily. While const can help prevent programming errors, it’s important to consider the specific needs of your code when deciding which keyword to use for variable declaration.

Scroll to Top