JavaScript Variables
What is JavaScript Variable
In JavaScript, a variable is an user defined name which is used as a container to store data values.
Variables are used to store and manipulate data in JavaScript program.
JavaScript variables are declared using var, let, or const keywords. The data type of a variable needs not to be specified explicitly in JavaScript.
JavaScript is a dynamically typed language. The type of a variable is determined by the value assigned to it.
Declaring JavaScript Variable
In JavaScript, variables can be declared using var, let, or const keywords before variable names.
- var: Older and general way to declare variables (example: var x = 10;). 'var' has function or global scope.
- let: It was introduced in ES6 (2015). let is preferred over var for most cases (example: let y = "Hello";). 'let' has block scope.
- const: It is used for constants. The value cannot be reassigned to a variable declared as constant. (example: const pie = 3.14;).
The following example demonstrates how to create variables using var, let, or const keywords:
Example of JavaScript variables
Output of the above example
10
Hello
3.14
27
Explanation of code
-
var x = 10;: This line declares a variable named 'x' using 'var' keyword and initializes it with value 10. -
console.log(x);: This line prints the value of 'x' variable in the console using 'console.log()' function. -
let y = "Hello";: This line declares a variable named 'y' using 'let' keyword and initializes the variable with value "Hello". -
console.log(y);: This line prints the value of 'y' variable in the console using 'console.log()' function. -
const pie = 3.14;: This line declares a constant variable named 'pie' using 'const' keyword and initializes the variable with value 3.14. -
console.log(pie);: This line prints the value of 'pie' variable in the console using 'console.log()' function. -
var num1, num2;: This line declares two variables named 'num1' and 'num2' using 'var' keyword without assigning any value to them. -
num1 = 12; num2 = 15;: The variables 'num1' and 'num2' are initialized with value 12 and 15. -
console.log(num1 + num2);: This line prints the sum of variables 'num1' and 'num2'.
JavaScript Variable Naming Rules
In JavaScript, variable names must follow the following specific rules to be valid:
- Variable names must start with a letter (A-Z, a-z) or an underscore (_), or a dollar sign ($). After the first character, variable names can contain letters, digits (0-9), underscores, or dollar signs.
- Reserved keywords (var, let, function, if, else, return, true, false, and others) cannot be used as variable names.
- Special characters like !, @, #, etc. (except the underscore '_' and dollar sign '$') cannot be used in variable names.
- JavaScript variables are case-sensitive. 'myNumber' and 'mynumber' are considered different variables.
- It is a good practice to use meaningful, descriptive, and case-sensitive variable names.
Some Examples of valid and invalid variables
JavaScript Compiler For Live Practice
Practice the above JavaScript code in the following JavaScript Editor. Create more JavaScript variables in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.
JavaScript Editor