ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
JavaScript Tutorial - Index
JavaScript Tutorial - Introduction to JavaScript
JavaScript Tutorial - Basic JavaScript Concepts
JavaScript Tutorial - Working with Operators and Variables
JavaScript Tutorial - How to work with Loops and Functions?
JavaScript Tutorial - How to work with Forms and Events?
JavaScript Tutorial - How to work with Objects?
JavaScript Tutorial - How to work with Browsers and Cookies?
JavaScript Tutorial - More about Objects
JavaScript Tutorial - How to validate Forms with JavaScript?
JavaScript Tutorial - Advanced Techniques in JavaScript
     

 
Online Personal Trainer Certification
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Business Analysis Course
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > JavaScript Tutorial > Working with Loops and Functions
 

JavaScript Tutorial - Working with Loops and Functions

 
Page 4 of 4
 
Passing Parameters

Functions may also be called with values, known as parameters, which may be used inside the body of the function. The function in which values are passed, is known as 'parameterized' function. Values are passed to the function by listing them in the parentheses following the function name. Both user-defined functions and built-in functions can accept parameters, process them and return values. During declaration, a function needs to be informed about the number of values that will be passed. Parameters exist only for the life of the function.

Example
var x;
function cube( x )
{
                   document.write ( " Enter the number: " + x );
                   var cube = x * x * x;
}


Calling Function

Once a function has been declared, it can be called (or invoked ) in a script anywhere in an HTML page. Function is usually called in the BODY tag of the HTML page, and you usually pass a parameter into the function. The number of parameters being passed should be equal to the number of arguments the function has.

Example
<html>
<head>
<SCRIPT LANGUAGE = "JavaScript">
var num;
function cube(num)
{
                return   num * num *num;
}
</SCRIPT>
</head>
<body>
{
               document.write(cube(5));
}
</body>
</html>

 

 
 
Summary
 

In this chapter you have learnt:

  • How looping helps controlling certain operations.
  • Different loop structures applicable at different situations.
  • Easier decision making through conditional constructs.
  • Skipping the loop construct through Break statement.
  • Accessing built-in functions.
  • Declaring and calling user-defined functions.

 

 

 
 
Review Questions
 

Fill in the Blanks

  1. ------- allows you to iterate a code in the program.
  2. ------- statement tests single expression, multiple times.
  3. The loop construct can be skipped using------- statement.
  4. Functions are declare and defined using the keyword----------.
Solutions
  1. Loop
  2. Switch
  3. break
  4. function

True or False

  1. JavaScript functions may have no parameters.
  2. In the For loop, update expression can be negative.
  3. Do-while will always execute a block of code, at-least once.
Solutions
  1. True
  2. True
  3. True
Sample Questions

 

 

Exercise
  1. Calculate factorial of a number.
  2. Calculate power to a number through function
  3. What is the difference between While and Do-while Loop.

 

Solution
  1. View Code     View Runtime
  2. View Code    View Runtime
  3. Do-while loop will always execute a block of code once, no matter the condition is valid or not, whereas while loop will execute the statements only if the condition is true
 
What's Next


The next chapter will acquaint you with the concepts of JavaScript Events. The chapter will further elucidate various event handlers available in JavaScript and how to pop-up dialog boxes in the window.

                   Hop over to the next chapter to get the close-up of JavaScript Events and Dialog boxes.

 
     

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2006. All Rights Reserved.