We touched on variables in the introduction of this course.
Variables are a very important concept to understand when it comes to programming in any language including
javascript unlike javascript constants.
Variables are named containers that can store a single value or multiple values.
These values can change based on user inputs or other factors.
Let's take a look at a basic example of how variables can be used to store values
in this javascript code.
We've declared three variables.
Variable X Y and Z.
You can think of each variable as a container the variables each contain their own value the value of
variable x is 10 the value of variable Y is 15.
The value of variable z is equal to the sum of variable x and y.
Which in this case would be twenty five or ten plus 15.
Now let's output the value of c Using the get element by IED output method will output the result in
a paragraph with the IED.
Demo
now let's save our file and preview it in our web browser to see the results.
Here we can see our heading javascript variables in our paragraph working with variables and underneath
that we can see the result has been output.
So variable X has been added to variable Y and the result has been output it to 25
in our example the X Y and Z name we attach to the variable are known as variable identifiers.
Each variable you declare must have a unique identifier we used very short identifiers but as we progressed
through the course we will be using more descriptive identifiers for example color shape size age and
weight these would all be considered more descriptive.
There are some general rules when selecting identifier names names can contain letters digits underscores
in dollar signs names must begin with a letter names are case sensitive.
Reserved words like Javascript keywords cannot be used as names.
Javascript variables can hold numbers like one hundred or ten point five or text values for example.
John Smith
text values are known as text strings.
Strings must be written in single or double quotes numbers can be written without quotes.
If you put quotes around a number it will be treated as a string
for the sake of this example.
Let's change our numerical value 15 to a text value.
Now when we save our file
and refresh our browser we can see that the numerical value 10 stored within the x value variable
has been attached to the string contained within variable Y.
The output is contained within variable Z.
Which equals X plus Y.