Stay tuned for additional content in this series.
Interactive code: When you see Run at the top of a code sample, you can run the code, check the results, make changes, and run it again.
Editorial note: This series has been updated with interactive code capabilities. When you see Run on a code listing, it means you can run the code, check the results, make modifications, and run it again.
- Arithmetic: Evaluates to a number
- String: Evaluates to a string
- Logical: Evaluates to a Boolean (true or false)
Types of variables
There are two types of variables: local and global. You declare local
variables using the
var keyword and global variables without
var keyword. With the
var keyword the
variable is considered local because it cannot be accessed anywhere
outside the scope of the place you declare it. For example, if you declare
a local variable inside a function
it cannot be accessed outside of that function, which makes it local to
that function. If you declare the same variable without the
var keyword, it is accessible throughout the entire script,
not only within that function.
A variable is a name that references a specific value. Use var followed by the variable's name to declare a variable, for example:
The above variable is declared, but it isn't defined (it does not reference a specific value). Here's an example of a defined variable, it references a specific value. in this instance the variable is 'some string':
var example = 'some string';
A string is a sequence of characters. Letters, numbers, punctuation marks, and spaces are all examples of characters. String values are surrounded by either single or double quotation marks.
'this is a string' "this is also a string"
Declaring a local variable
The local variable in this example is named
num. It is
assigned the value of 10.
var num = 10;
To access the value of the
num variable at another point in
the script, reference the variable by name, as shown in the interactive
Click Run to see the results of running the code as is. Change the variable and click Run again.
To store arithmetic expressions in a variable, assign the variable to the
calculated value, for example
var num = (5 + 5);. The result of the calculation is what is
stored in the variable, not the calculation itself.
Click Run to see the results of running the code as is. Change the variable or the operator and click Run again.
Changing variable values
To change the value of a variable, refer to the variable by the name you
assigned to it (in this case
num) and assign it a new value using the
equal sign, as shown below. The difference this time is that you do not
have to use the
var keyword because the variable has already
Click Run to see the results of running the code as is> As written, the result of this script is "The value of num is: 10" followed by "The new value of num is: 15." Now change the variables and click Run again.
Now it's time to put what you've learn to practice. You'll add strings
together using two methods in the next two examples. First you'll use the
+ to add the strings together. Then, you'll use the
Click Run to see the script add the strings together and print the result. Change the strings, the operator, or other parts of the code to see what happens.
your first name and last name to the variables and add the variables
var fullName, then click Run to
print your full name.
Summary and next steps
- Java development hub on developerWorks
- Intro to Java Programming learning path
- More tutorials for Java developers
- Jump start your project with with journeys on developer.ibm.com/code