Getting started with understanding JavaScript variables and getting started with javascript
Variables are containers used to store information:
x=5; length=66.10;
Do you still remember the Algebra I learned at school?
When you recall the algebra courses you have learned at school, you may think of x = 5, y = 6, z = x + y, and so on.
Remember, a letter can save a value (for example, 5), and the above information can be used to calculate that the value of z is 11.
You must have forgotten it, right.
These letters are called variables and can be used to save values (x = 5) or expressions (z = x + y ).
JavaScript Variables
Like algebra, JavaScript variables are used to save values or expressions.
You can give the variable a brief name, such as x or a descriptive name, such as length.
JavaScript variables can also save text values, such as carname = "Volvo ".
Rules for JavaScript variable names:
Variables are case sensitive (y and Y are two different variables)
The variable must start with a letter or underscore
Note: JavaScript is case sensitive and variable names are also case sensitive.
Instance
You can change the value of a variable during script execution. You can reference a variable by its name to display or change its value.
This example shows how it works.
Declare (create) JavaScript Variables
Variable creation in JavaScript is often called a "Declaration" variable.
You can declare JavaScript variables using the var statement:
var x;var carname;
After the preceding Declaration, variables have no values, but you can assign values to them when declaring them:
var x=5;var carname="Volvo";
Note: When assigning a text value to a variable, enclose the value with quotation marks.
Assign values to JavaScript Variables
Assign values to JavaScript variables using the assignment statement:
x=5;carname="Volvo";
The variable name is on the left of the = symbol, and the value to be assigned to the variable is on the right of the = symbol.
After the preceding statement is executed, the value saved in variable x is 5, and the carname value is Volvo.
Assign values to undeclared JavaScript Variables
If the variable you assign has not been declared, the variable is automatically declared.
These statements:
x=5;carname="Volvo";
The effects of these statements are the same:
var x=5;var carname="Volvo";
Redeclare JavaScript Variables
If you declare a JavaScript variable again, the variable will not lose its original value.
var x=5;var x;
After the preceding statement is executed, the value of variable x is still 5. When the variable is declared again, the value of x is not reset or cleared.
JavaScript Arithmetic
Like algebra, you can use JavaScript variables for arithmetic:
y=x-5;z=y+5;