Contents


Get started with the JavaScript language, Part 2

Variables

Comments

Content series:

This content is part # of 7 in the series: Get started with the JavaScript language, Part 2

Stay tuned for additional content in this series.

This content is part of the series:Get started with the JavaScript language, Part 2

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.

Variables are something to get familiar with. They store data that can later be retrieved or updated with new data. The data stored in a variable can be a value or expression. There are three types of expressions in the JavaScript language:

  • 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 using the 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:

var 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 listing below.

Your turn!

Try iconClick Run to see the results of running the code as is. Change the variable and click Run again.

Show result

Storing expressions

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.

Your turn!

Try iconClick Run to see the results of running the code as is. Change the variable or the operator and click Run again.

Show result

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 been declared.

Your turn!

Try iconClick 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.

Show result

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 operator + to add the strings together. Then, you'll use the concat() method.

Your turn!

Try icon 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.

Show result

Your turn!

Try iconAssign your first name and last name to the variables and add the variables together for var fullName, then click Run to print your full name.

Show result

In addition to teaching about variables, this section introduced the next topic, which is operators. The equal sign (=), which you used to assign values to the variables, is an assignment operator, and the plus sign (+), which you used to add 5 + 5, is an arithmetic operator. Part 2 of this series is about all of the available operators in the JavaScript language and how you can use them.

Summary and next steps

This tutorial introduced you to variables and scripts which are some of the fundamentals of the JavaScript language. Continue your journey of learning one of the most popular programming languages with part 3 of this 8-part series, operators.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=1044809
ArticleTitle=Get started with the JavaScript language, Part 2: Variables
publish-date=05102017