Javascript Variables

share link

by vsasikalabe dot icon Updated: Feb 24, 2023

technology logo
technology logo

Guide Kit Guide Kit  

A JavaScript variable is used to store the value. You can initialize it with a value once you have declared a variable. To initialize a variable, you can specify the variable name, followed by an equal's sign (=). After that, you can change its value by assigning a different value.  

While declaring a JavaScript variable, we have some rules (also known as identifiers). 

  • It must start with an underscore (), a letter (a to z or A to Z), or dollar ($) sign. 
  • After the first letter, we can use digits (0 to 9), for example, value 3. 
  • JavaScript variables are case-sensitive, i.e., a and A are different variables. 

JavaScript local variable 

A JavaScript local variable is declared value within the block. We can access it within the function or block only. 

JavaScript global variable 

A variable is declared outside the function or with a window so that we can access it from any function.  

4 Ways to Declare a JavaScript Variable: 

  • var- The var statement declares a value globally scoped variable. 
  • let - It declares variables limited to the scope of a block statement where it is used. 
  • const -A constant holds a value that does not change.  
  • nothing 

Start the statement with let and separate the variables by a comma. We can declare many variables in one statement. If you re-declare a JavaScript variable declared with var, it will not lose its value. 

Difference between undefined and undeclared variables. 

An undefined variable has been declared but not initialized with a value. For example: 

     let message; 

     console.log(message); // undefined 

Undeclared variables are always global. It has not been declared using var, let, or const keywords. 

Here is an example of how to create Javascript Variables: 

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



Follow the steps carefully to get the output easily.

  1. Download and Install VS Code on your Desktop.
  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
  4. Copy the code using the "Copy" button above, and paste it in your javascript file.
  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 ' console input output javascript variables' 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.11.8

Using this solution, we are able to use Javascript Variables 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 use Javascript Variables.


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