Declaration of a variable (let and const) in javascript ES6

share link

by vsasikalabe dot icon Updated: Mar 16, 2023

technology logo
technology logo

Guide Kit Guide Kit  

In JavaScript, users can declare a variable using three keywords (var, let, and const). The let variable is declared using the let keyword accessed within the block scope. Block scope is nothing but a section or function{}, a block{}, or a global (script) where the let variable gets declared. The const variables in ES6 have a new way to declare a constant using the const keyword.  


The keyword const is created and has a read-only reference to the value. The const variable has some properties that are as follows: 

Properties: 

  • We cannot reassign a value. 
  • It has a block scoped. 
  • A constant cannot be re-declared. 
  • It must be initialized at the time of declaration. 

Global Scope: We can access the variable from any part or section of the JavaScript code in Global scope. 

Local Scope: We can access the variable within a function in the local scope where it is declared. 

They are all hoisted to the top of their function or scope. Const must be initialized during declaration. Without being initialized, var and let can be declared. 

Note these points before declaring a variable

  • Don't use var anymore. 
  • Use let or const. 
  • Use const more often.  
  • You can use the let keyword when you need to reassign another value to a variable. 
  • Only try to access a variable after declaring it. 


Here is an example of how to declare Variables (let and const) using Javascript ES6: 

Preview of the output that you will get on running this code from your IDE.

Code

Instructions

Follow the steps carefully to get the output easily.

  1. Download and Install VS Code on your Computer.
  2. Open the VS Code and install Javascript ES6 Code Snippet and Code Runner from Extensions.
  3. Open the new file and save as example.js (functions.js)
  4. Copy the code using the "Copy" button above, and paste it in your Javascript file. (First Run Const variable and next do it for Let variable)
  5. Run the code using (ctr+alt+N) Run button.


I hope you found this useful. I have added version information in the following sections.


I found this code snippet by searching for ' Declaration of a variable (let and const) in javascript ES6' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. JavaScript (ES6) code snippets v1.8.0
  3. Code Runner v0.12.0


Using this solution, we are able to declare of a variable (let and const) in javascript ES6 with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to declare of a variable (let and const) in javascript ES6.

Support

  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page