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
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

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

JavaScript Tutorial - Working with Loops and Functions

 
Page 3 of 4
 
Continue Statement

The continue statement terminates the current iteration and executes next statement. It does not terminate the loop statement, but it allows the JavaScript statements to skip after the continue statement. The control still remains inside the loop. The loop is not exited, as in a break statement. When the keyword 'continue' is encountered inside any JavaScript loop, control automatically passes to the beginning of the loop.

Example
var x;
for( x = 0 ; x <= 50 ; x++ )
{
            if ( x % 5!= 0 )
           {
                continue;
           }
           else
           {
                 document.write (" 5 is divisible to" + x );
           }
}

 

Functions in JavaScript

Functions are blocks of JavaScript code that perform a specific task and often returns a value. A function is a block of code that has a name. Whenever that name is used, the function is called, which means that the code within that function is executed. A JavaScript function may take zero or more parameters. Parameters are a standard technique via which control data can be passed to a function. Control data that is passed to a function, offers means of controlling what a function returns. Function serve two purposes :

  • A function permits you to perform the same operation, number of times, without simply copying the same code.
  • JavaScript functions link actions on a web page with JavaScript code. For example, Mouse clicks, button presses, text selections and other user actions can call JavaScript functions.

Built - in Functions

JavaScript provides several built-in functions that are built into an application and can be accessed by end-users without writing code. Some of them are described below :

eval ( )

The eval( ) function takes a string as its argument. The string can represent JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. The eval( ) function converts a string to integer or float value. This function is useful for evaluating a string representing an arithmetic expression.

Example
var result = eval ("20 * 5 + 5");
The output assigned to the variable 'result' will be : 10

parseInt ( )

This function parses the string (its first argument) and attempts to return an integer . The parseInt ( ) function returns the first integer contained in a string or 0, if the string does not begin with an integer. If parseInt ( ) function encounters a character that is not a numeral in the specified radix (base), it ignores it and all succeeding characters and returns the integer value up to that point.

Example
var result = parseInt ("367gt")
The output in the variable 'result' will be: 367

parseFloat ( )

This function returns the first floating point number contained in a string or 0, if the string does not begin with a valid floating point number. If parseFloat ( ) function encounters a character other than a sign ( + or -), a numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that characters and all succeeding characters.

Example
var result = parseFloat ("22.9xyz")
The output in the variable 'result' will be : 22.9

User - defined Functions

A user-defined function needs to be declared and coded. Once the function is declared and coded, the function can be invoked by calling it, with the name given to it. Functions can accept information in the form of arguments and can return results.

Declaring Functions

Functions are declared and defined using the keyword 'function'. A function consists of :

  • Function name
  • List of parameters ( arguments ), that will accept values, are passed to the function when called.
  • Block of JavaScript code that defines what the function does.
Syntax
function  function_name ( parameter1, parameter2,.....parameter n)
                              {
                                         JavaScript Statements;
                              }

                                  
A function_name is case sensitive and should be unique. The list of parameters passed to the function should be separated by comma. A function can also be declared without having any parameters. But parentheses must be included after the function_name. The JavaScript statements are the function's body that actually make up the function.
 
A function is placed inside the HEAD of HTML document. This ensures that all functions will be parsed before they are invoked or called. If the function is called before it is declared and parsed, it will lead to an error condition, as the function has not been evaluated and the 'Browser' does not know that it exists.

 

     

 

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