Practice: Getting started with JavaScript

The JavaScript language includes basic constructs that are commonly used in any script you write. Get the hands-on practice you need to store and retrieve values, create logic, iterate arrays, and write functions—all of which give you a foundation for writing custom code in JavaScript.

Kris Hadlock (kris@krishadlock.com), Web Developer/Designer, Studio Sedition

Photo of Kris HadlockKris 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.



01 November 2011

Also available in Japanese

About this article

These exercises and solutions supplement the developerWorks articles "Get started with the JavaScript language, Part 1: JavaScript language fundamentals" and "Get started with the JavaScript language, Part 2: Events, cookies, timing, and more," which are part of the developerWorks knowledge path "A comprehensive guide to JavaScript." You may want to read these articles before working through these exercises.

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 var keyword to create a variable and retrieve the variables value using the document.write function.

Exercise 2: Using operators

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.

Exercise solutions

Follow these solution steps to check your work.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Web development on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=767553
ArticleTitle=Practice: Getting started with JavaScript
publish-date=11012011