The exercises in this article give you practice in:
- Creating and using variables
- Using arithmetic operators
- Using assignment operators
- Using comparison operators
- Creating and using arrays
- Creating loops and iterating arrays
- Creating and using functions
To work through the exercises in this article, you should have an HTML editor and a working Internet connection or localhost environment in which to test your JavaScript.
Exercise 1: Creating variables
Variables are the building blocks for any programming language. They provide a way to store and retrieve information later in a script. For this exercise:
- Use the
varkeyword to create a variable and retrieve the variables value using thedocument.writefunction.
Operators are necessary when performing any operation in JavaScript. Operations include addition, subtraction, and comparison. For this exercise:
- Use an arithmetic operator to perform a basic mathematic operation and use it as a variable value.
- Use comparison operators to determine the relationship between variables and their values.
Exercise 3: Creating an array and using a loop to iterate its values
You use arrays to store multiple values in a single variable. You can use loops to iterate arrays and access their values. For this exercise:
- Create an array and assign values to it.
- Create a for loop and use it to iterate the array.
Exercise 4: Creating functions, accepting parameters, and returning values
Functions contain script that can be used to handle a specific task. You can use functions to execute their contained script and run their tasks at any time. For this exercise:
- Structure a custom function named
increase. - Include a function parameter named
_num. - Increase the value of the parameter and return the new value.
Follow these solution steps to check your work.
-
Learn more about JavaScript
language fundamentals.
-
Learn how to create Events,
cookies, timing, and more using JavaScript.
-
Stay current with developerWorks
technical events and webcasts focused on a variety of IBM products and IT
industry topics.
-
Follow developerWorks on Twitter.

Kris Hadlock has been a contract web developer and designer since 1996. He has worked on projects for companies such as SPIN Magazine, IKEA, United Airlines, JP Morgan Chase, GoDaddy Software, and Fire Mountain Gems. He is the author of Ajax for Web Application Developers (Sams) and The ActionScript Migration Guide (New Riders) as well as a featured columnist and writer for numerous websites and design magazines, including Peachpit.com, InformIT.com, and Practical Web Design magazine. Kris is also the founder of www.studiosedition.com, a web design and software development studio specializing in fusion of form and function.



