Declare Variables Using Const In JavaScript
The const keyword has been to declare variables since ECMAScript 2015 (ES6).
JavaScript variables can be declared in 4 ways. Automatically, using var, let or const.
Const is used to declare variables in a block scope with a Temporal Dead Zone (TDZ).
Variable Declaration
Glossary:
Scope
Refers to region of the code where variable is accessible.
Global Scope
Variables declared outside function or block are accessible anywhere.
Block Scope
Variables declared within block such as if or for statement are only accessible in that local scope.
Function Scope
Variables declared within function are only accessible in that local scope.
Initialization
Variables are initialized with a default value.
Immutable
Variables values cannot be changed after initialization.
Temporal Dead Zone
Variables are not accessible until execution reaches declaration point.
Declaring Variables
Name | Description | Example |
---|---|---|
const | Declares block scope with mandatory initialization | const num = 1; |
Name | Description | Example |
Const Variable Declaration Snippet
// OjamboShop.com Learning JavaScript Const Variable Declaration Tutorial // Global Scope const num1 = 1; console.log(num1); // Inaccessible Outside Block if (true) { console.log(num1); const num2 = 2; console.log(num2); } // Immutable const num2 = num1; console.log(num2);
Usage
You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE was used to input and compile JavaScript code for the const variable declaration.
Open Source
JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.
Learn Programming Courses:
Courses are optimized for your web browser on any device.
Limited Time Offer:
OjamboShop.com is offering 20% off coupon code SCHOOL for Learning JavaScript Course until End Day 2024.
Learn Programming Ebooks:
Ebooks can be downloaded to your reader of choice.
Conclusion:
JavaScript makes it easy to declare variables. Use const to declare immutable block scoped variables with the mandatory initialization.
Take this opportunity to learn the JavaScript programming language by making a one-time purchase at Learning JavaScript Course. A web browser is the only thing needed to learn JavaScript in 2024 at your leisure. All the developer tools are provided right in your web browser.
If you prefer to download ebook versions for your reader then you may purchase at Learning JavaScript Ebook
References:
- Learning JavaScript Course on OjamboShop.com
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning JavaScript Ebook on Amazon
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials