A variable in JavaScript is used to a store data value that may be changed later on. It is essentially a name of storage location. There are two types of variables in JavaScript, and those include; local variable and global variable.
Three rules to follow when declaring a JavaScript variable:
- Name must start with a letter (a-z or A-Z), underscore( _ ), or dollar( $ ) sign.
- After the first letter, use digits (0-9)
- JavaScript variables are case sensitive (i.e: a and A are different variables because one is lowercase, and the other is uppercase)
A local variable in JavaScript is declared within a block or function, and is only accessible within that function or block.
A global variable in JavaScript is accessible from any function.
You can declare a variable in JavaScript using “var“, “const“, or “let“.