JavaScript Variables
Last Updated : 23 Jan 2026
In JavaScript, a variable is a named storage location that holds a value, which can be any data type, such as numbers, strings, objects, etc. It can be declared using keywords like var, let, or const.
- Variables as Placeholders for unknown values: They can be used in places where the values they represent are unknown when the code is written.
- Variables as Code Clarifies: They can make the purpose of your code cleaner.
Rules of Naming Variables in JavaScript
There are some rules while declaring a JavaScript variable (also known as identifiers).
- Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
- After the first letter, we can use digits (0 to 9), for example, value1.
- JavaScript variables are case-sensitive; for example, x and X are different variables.
Declaring Variables in JavaScript
You can use the "var" or "let" keywords to declare text as a variable. There are three methods to declare a variable in JavaScript:
Using var keyword
In JavaScript, variables were traditionally declared using the var keyword. It is not used in contemporary programming due to certain problems with scope and complicated operations.
Using let keyword
The variables with block scope are declared using the let keyword. It is only available within the block in which they are specified in the JavaScript function.
Using const keyword
Constant variables are used to avoid modification, and they cannot change value after they are declared variables. It is a represented variable using the const keyword in JavaScript.
JavaScript Variable Data Types
Different types of data can be stored in JavaScript variables. These are the primary types:
- String: Strings represent text values.
- Number: The number represents numerical values.
- Boolean: True or false is represented by boolean.
- Undefined: A declared variable without a value given to it.
- Null:It indicates a purposefully empty value.
- Object: Key-value pairs are represented by objects.
- Array: A collection of values is represented by an array.
Types of Variables
The types of variables are used according to operation and values. If you have to use a single value multiple times, then use global; otherwise, use the local variable. There are following two types of variables in JavaScript:
- Local variable
- Global variable
JavaScript Local Variable
JavaScript local variables are declared inside the curly braces {} or a function. In JavaScript, variables are accessible within the function or block only where they are declared.
Local variables with the same name can be used in different functions or blocks. Local variables are deleted after the function is completed.
Output:
TpointTech ReferenceError: word is not defined
Output:
Tpoint Tech Tpoint undefined
JavaScript Global Variable
In JavaScript, Global variables are those variables that are declared outside of any function and can be accessible from anywhere within the script, including these functions.
Example:
The example below describes the global variable operation.
Output:
lobal value of Grade is: B local value of Grade is: A
The Best Ways to Use JavaScript Variables
Use these best practices for writing JavaScript variable code:
- Use let and const: Avoid using var to avoid scope-related problems by using let and const instead.
- Use meaningful variable names: Select names that are descriptive and help the reader understand the code.
- Use the camelCase naming convention: JavaScript variables should be named using the camelCase format (myVariableName as an example).
- Use const: when the variable should not change or unwanted modifications should be avoided.
- Declare variables at the start of a block: the variable declaration is used to improve readability and prevent problems with hoisting.
- Clear the global variables: use local variables whenever feasible to avoid unforeseen changes.
Conclusion
JavaScript variables are necessary for a program's data storage and manipulation. Writing effective and error-free code requires an understanding of its definition, scope, hoisting, and recommended practices. Students can improve their programming abilities and build a solid foundation in JavaScript by adhering to the principles and best practices covered in this tutorial.