JavaScript Variable
JavaScript Variable
A JavaScript variable is a container for storing data values. It allows you to name and store a piece of information so it can be reused or manipulated in your program. Variables are declared using keywords like var, let, or const and can hold different types of data such as numbers, strings, objects, or arrays.
In JavaScript, you can create variables using the following keywords:
1. var - Used in older versions of JavaScript (ES5 and earlier). It is function-scoped and can be redeclared.
var name = "Alice"; console.log(name); // Output: Alice name = "Bob"; // Reassigning is allowed console.log(name); // Output: Bob
2. let - Introduced in ES6, let is block-scoped and cannot be redeclared in the same scope.
let age = 25; console.log(age); // Output: 25 age = 26; // Reassigning is allowed console.log(age); // Output: 26 // let age = 30; // Error: Cannot redeclare block-scoped variable 'age'
const - Introduced in ES6, const is block-scoped and used for variables whose values will not change.
const PI = 3.14159; console.log(PI); // Output: 3.14159 // PI = 3.14; // Error: Assignment to constant variable
Guidelines for Choosing
- Use const by default unless you plan to reassign the variable.
- Use let if reassignment is required.
- Avoid var in modern JavaScript because of its quirks (e.g., hoisting and lack of block scope).
Rules for Naming JavaScript Variables
-
Valid Characters:
- Variable names can include letters (
a-z
,A-Z
), digits (0-9
), underscores (_
), and dollar signs ($
). -
The name must not start with a digit.
Example:let validName1 = "Hello"; // Valid let $price = 100; // Valid let _name = "John"; // Valid let 1name = "Invalid"; // Invalid
- Variable names can include letters (
-
Reserved Keywords:
-
Variable names cannot use JavaScript reserved keywords like
var
,let
,class
,return
,function
, etc.
Example:let let = "Invalid"; // Invalid let functionName = "Valid"; // Valid
-
Variable names cannot use JavaScript reserved keywords like
-
Case Sensitivity:
-
JavaScript variable names are case-sensitive.
Example:let studentName = "Alice"; let StudentName = "Bob"; // Different variable from `studentName`
-
JavaScript variable names are case-sensitive.
-
No Spaces:
-
Variable names cannot contain spaces.
Example:let user Name = "Invalid"; // Invalid let userName = "Valid"; // Valid
-
Variable names cannot contain spaces.
-
Must Start with a Letter,
_
, or$
:-
A variable name must start with a letter, an underscore (
_
), or a dollar sign ($
). Example:let _age = 25; // Valid let $total = 500; // Valid let 9number = "Invalid"; // Invalid
-
A variable name must start with a letter, an underscore (
-
Avoid Special Characters:
-
Special characters other than
_
and$
are not allowed.
Example:let name# = "Invalid"; // Invalid
-
Special characters other than
Best Practices
-
Descriptive Names:
-
Use meaningful and descriptive names.
Example:let userAge = 30; // Descriptive let x = 30; // Not descriptive
-
Use meaningful and descriptive names.
-
Camel Case:
-
Use camelCase for variable names (common convention in JavaScript).
Example:let firstName = "Alice"; // Camel Case let firstname = "Valid but less readable"; // Avoid this
-
Use camelCase for variable names (common convention in JavaScript).
-
Avoid Single-Letter Names:
-
Except in loop counters (
i
,j
, etc.), avoid single-letter names. Example:let totalCost = 200; // Descriptive let t = 200; // Not descriptive
-
Except in loop counters (
-
Do Not Use Global Variables:
- Avoid creating variables in the global scope unnecessarily.
-
Avoid Confusion:
- Do not use names that are too similar to each other (e.g.,
o
,O
,l
,1
).
- Do not use names that are too similar to each other (e.g.,
Valid and Invalid Variable Names Examples
Variable Name | Validity | Reason |
---|---|---|
userName |
✅ Valid | Starts with a letter, uses camelCase. |
_userAge |
✅ Valid | Starts with an underscore. |
$price |
✅ Valid | Starts with a dollar sign. |
1stPlace |
❌ Invalid | Cannot start with a digit. |
var |
❌ Invalid | var is a reserved keyword. |
user-Name |
❌ Invalid | Contains a hyphen (- ). |
user name |
❌ Invalid | Contains a space. |