JavaScript Variables
Learn about naming conventions and data types and how to declare and assign values to JavaScript variables.
There are four 4 Ways to Declare a JavaScript Variable:
- Using var
- Using let
- Using const
- Using nothing
What are Variables?
JavaScript variables store data. Therefore you can apply them to temporarily save the values your program needs to operate. Think of them as little containers or boxes that hold a specific piece of data that you specify.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Variables Example</h2> <p> a, b, and c are variables.</p> <p id="test"></p> <script> var a = 7; var b = 10; var c = a + b; document.getElementById("test").innerHTML = "The value of c is: " + c; </script> </body> </html>
In the above example, a, b, and c, are three variables and declared these variables with the var keyword.
Declaring variables
To create a variable you must declare it with the var keyword. The var keyword tells the JavaScript interpreter to create a new variable. Let’s create a variable named a, b and c.
Example
var a var b
In the above example, a and b are two variables and declared these variables with the var keyword.
The Assignment Operator
After you declare a variable with var, you need to assign a value, until you do the variable has a value equal to undefined.
In practical terms, it means that the variable has no value assigned to it.
To assign a value to a variable use the equal sign =
Example
a = 7; b = 8;
Notice how you didn’t use the var keyword here. You already declared the variable in the code above. Here you are assigning a value to an existing variable.
Declaring and assigning a value at the same time
If you are declaring a variable for the first time and know the value that should be assigned to it. You can use a one-line declaration and assignment statement:
Example
var a = 7; var b = 8; var c = a + b;
In the above example, a, b, and c, are three variables and declared these variables with the var keyword.
JavaScript Identifiers
All JS variables must be specified with unique names.
These unique names are contacted identifiers.
You must follow certain rules when declaring (naming) variables in JavaScript. JavaScript variable names must adhere to all the following rules:
- Must begin with a letter, dollar sign $ or underscore _.
- Can contain letters, digits, and the special characters $ and _ only.
- There are certain reserved words that you cannot use, like var for example.
- Remember that JavaScript is a case-sensitive language, this also applies to variables. Car isn’t the same as car.
When declaring variables try to use descriptive names. The variable name x is not descriptive at all, but the name carHasAirbag, tells you what kind of data this variable may have.
Data types
JavaScript variables can contain numbers( like 200) and text values ( like “Mary Doe”) also.
JavaScript can handle many different data types. A data type is a group of data with values having predefined characteristics.
Strings are composed inside double or single quotes. Digits are composed without quotes. As an example, a string data type can contain alphanumeric characters.
<!DOCTYPE html> <html> <body> <h2>JavaScript Data types</h2> <p>Strings and Numbers data type expamples</p> <p id="test"></p> <script> const pi = 3.14; let human = "Mary Doe"; let reply = 'Yes I am!'; document.getElementById("test").innerHTML = pi + "<br>" + human + "<br>" + reply; </script> </body> </html>