JavaScript Variables
In this tutorial you will learn how to create variables to store data in JavaScript.
What is Variable?
Variables are fundamental to all programming languages. Variables are used to store data, like string of text, numbers, etc. The data or value stored in the variables can be set, updated, and retrieved whenever needed. In general, variables are symbolic names for values.
You can create a variable with the var
keyword, whereas the assignment operator (=
) is used to assign value to a variable, like this: var varName = value;
Example
Try this code »var name = "Peter Parker";
var age = 21;
var isMarried = false;
Tip: Always give meaningful names to your variables. Additionally, for naming the variables that contain multiple words, camelCase is commonly used. In this convention all words after the first should have uppercase first letters, e.g. myLongVariableName
.
In the above example we have created three variables, first one has assigned with a string value, the second one has assigned with a number, whereas the last one assigned with a boolean value. Variables can hold different types of data, we'll learn about them in later chapter.
In JavaScript, variables can also be declared without having any initial values assigned to them. This is useful for variables which are supposed to hold values like user inputs.
Example
Try this code »// Declaring Variable
var userName;
// Assigning value
userName = "Clark Kent";
Note: In JavaScript, if a variable has been declared, but has not been assigned a value explicitly, is automatically assigned the value undefined
.
Declaring Multiple Variables at Once
In addition, you can also declare multiple variables and set their initial values in a single statement. Each variable are separated by commas, as demonstrated in the following example:
Example
Try this code »// Declaring multiple Variables
var name = "Peter Parker", age = 21, isMarried = false;
/* Longer declarations can be written to span
multiple lines to improve the readability */
var name = "Peter Parker",
age = 21,
isMarried = false;
The let and const Keywords ES6
ES6 introduces two new keywords let
and const
for declaring variables.
The const
keyword works exactly the same as let
, except that variables declared using const
keyword cannot be reassigned later in the code. Here's an example:
Example
Try this code »// Declaring variables
let name = "Harry Potter";
let age = 11;
let isStudent = true;
// Declaring constant
const PI = 3.14;
console.log(PI); // 3.14
// Trying to reassign
PI = 10; // error
Unlike var
, which declare function-scoped variables, both let
and const
keywords declare variables, scoped at block-level ({}
). Block scoping means that a new scope is created between a pair of curly brackets {}
. We'll discuss this in detail later, in JavaScript ES6 features chapter.
Note: The let
and const
keywords are not supported in older browsers like IE10. IE11 support them partially. See the JS ES6 features chapter to know how to start using ES6 today.
Naming Conventions for JavaScript Variables
These are the following rules for naming a JavaScript variable:
- A variable name must start with a letter, underscore (
_
), or dollar sign ($
). - A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters (
A-z
,0-9
) and underscores. - A variable name cannot contain spaces.
- A variable name cannot be a JavaScript keyword or a JavaScript reserved word.
Note: Variable names in JavaScript are case sensitive, it means $myvar
and $myVar
are two different variables. So be careful while defining variable names.