const myConstant = 10;
In this example, we declare a variable called "myConstant" and assign it the value of 10. Once you've declared a constant using "const", you cannot change its value. For example, if you try to reassign a new value to "myConstant", you'll get an error:
const myConstant = 10; myConstant = 20; // Error: Assignment to constant variable.
This is because "myConstant" is a constant variable and cannot be reassigned a new value.
You can also declare and assign multiple constant variables at once, like this:
const myConstant1 = 10, myConstant2 = "Hello", myConstant3 = true;
It's important to note that while you cannot reassign a new value to a constant variable, the value itself can still be mutable. For example, if you declare a constant variable that contains an array or an object, you can still modify the contents of that array or object:
const myArray = [1, 2, 3]; myArray.push(4);
OK, can modify the contents of the array
However, you cannot reassign a new value to "myArray":
const myArray = [1, 2, 3]; myArray = [4, 5, 6]; // Error: Assignment to constant variable.
In general, it's a good practice to use "const" by default when declaring variables, unless you know that you need to reassign the variable later. This can help prevent accidental reassignments and make your code more predictable.