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