Back
Next
Many web developers enjoy using VBScript, due to its many nice,
inherent functions. However, Internet Explorer is the only browser
which supports VBScript, so for production of web applications it is
essential that all client-side scripting be written in JavaScript only.
What Is a JavaScript Function?
Microsoft JavaScript functions perform actions. JavaScript Functions can also return results.
Sometimes these are the results of calculations or comparisons like mathematical
operations.
JavaScript
Functions combine several operations under one name. This lets us streamline
our JavaScript code. We can write out a set of JavaScript statements, name it, and then execute the
entire set any time we want to, just by calling it and passing to it any information it needs.
No in JavaScript Tutorial we will learn that we pass information to a JavaScript
Function by enclosing the information in parentheses
after the name of the JavaScript Function. Pieces of information that are being passed to
a JavaScript Function are called arguments or parameters of JavaScript Function. Some
JavaScript Functions don't take any
arguments at all, some functions take one argument; some take several. There
are even functions for which the number of arguments depends on how we are
using the JavaScript Function.
JavaScript supports two kinds of functions: those that are built into the language,
and those we create ourself.
Built-in Functions in JavaScript
The JavaScript language includes several built-in functions. Some of them let
us handle expressions and special characters, and convert strings to numeric values.
For example, escape() and unescape() are used to convert characters that have
special meanings in HTML code, characters that
we cannot just put directly into
text. For example, the angle brackets, "<" and ">", delineate
HTML tags.
The escape JavaScript Function takes as its argument any of these special characters, and
returns the escape code for the character. Each escape code consists of a
percent sign (%) followed by a two-digit number. The unescape()
JavaScript Function
is the exact inverse. It takes as its argument a string consisting of a
percent sign and a two-digit number, and returns a character.
Another useful built-in JavaScript Function is eval(), which evaluates any valid
mathematical expression that is presented in string form. The eval()
function of JavaScript takes one argument, the expression to be evaluated.
Example of Built-in JavaScript Functions
var anExpression = "6 * 9 % 7";
var total = eval(anExpression);
// Assigns the value 5 to the variable total.
var yetNewExpression = "6 * (9 % 7)";
total = eval(yetNewExpression)
// Assigns the value 12 to the variable total.
var totality = eval("Well come at Global Guide Line JavaScript Tutorial.");
// Generates an error.
Creating our own Functions in JavaScript
We can create our own JavaScript functions and use them where we need them.
A JavaScript function definition consists of a function statement and a block of
JavaScript statements.
The checkTripletFunction function in the following example takes as its arguments
the lengths of the sides of a triangle, and calculates from them whether
the triangle is a right triangle by checking whether the three numbers
constitute a Pythagorean triplet. The square of the length of the hypotenuse
of a right triangle is equal to the sum of the squares of the lengths of the
other two sides. The checkTripletFunction function calls one of two other functions to
make the actual test.
Notice the use of a very small number "epsilon" as a testing
JavaScript variable in the
floating-point version of the test. Because of uncertainties and roundoff errors
in floating-point calculations, it is not practical to make a direct test of whether
the square of the hypotenuse is equal to the sum of the squares of the other
two sides unless all three values in question are known to be integers. Because
a direct test is more accurate, the code in this JavaScript Function example determines whether
it is appropriate and, if it is, uses it. Lets see the code here in JavaScript
Tutorial.
Example of JavaScript Function
var epsilon = 0.0000000000001;
// Some very small number to test against.
var triplet = false;
function integerCheck(a, b, c)
{ // The test function for integers.
if ( (a*a) == ((b*b) + (c*c)) ) { // The test
itself.
triplet = true;
}
} // End of the integer checking function.
function floatCheck(a, b, c)
{ // The test function for
floating-point numbers.
var theCheck = ((a*a) - ((b*b) + (c*c))) // Make the test number.
// The test requires the absolute value, so invert theCheck
if it's negative.
if (theCheck < 0) {
theCheck *= -1;
}
if (epsilon > theCheck) { // If it's as close as
that, it's pretty darn close!
triplet = true;
}
} // End of the floating-poing check function.
// The triplet checker. First, move
the longest side to position "a".
function
checkTripletFunction(a, b, c)
{
var d = 0; // Create a temporary holding bin.
if (c > b) { // If c > b, swap them.em.
d = c;
c = b;
b = d;
} // If not, ignore them.
if (b > a) { // If b > a, swap them.
d = b;
b = a;
a = d;
} // If not, ignore them.
// Side "a" is now the hypotenuse, if there is one.
if (((a%1) == 0) && ((b%1) == 0) && ((c%1) == 0)) {
// Test all 3 values. Are they integers?
integerCheck(a, b, c); // If so, use the precise check.
}
else
floatCheck(a, b, c); // If not,
get as close as is reasonably possible.le.
}< // End of the triplet check
JavaScript function.
// The next three statements assign sample values for testing purposes.
var sideA = 5;
var sideB = 5;
var sideC = Math.sqrt(50);
checkTripletFunction(sideA, sideB, sideC); // Call the function. After the call,
triplet contains the result.lt.
Back
Next
|