Home | Javascript validator | JavaScript Editor JavaScript EditorGet Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.
Microsoft® JScript®
JavaScript Functions
 JScript Tutorial
Previous
Next


What Is a Function?
Microsoft JavaScript functions perform actions. They can also return results. Sometimes these are the results of calculations or comparisons.

Functions combine several operations under one name. This lets you streamline your code. You can write out a set of statements, name it, and then execute the entire set any time you want to, just by calling it and passing to it any information it needs.

You pass information to a function by enclosing the information in parentheses after the name of the function. Pieces of information that are being passed to a function are called arguments or parameters. Some 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 you are using the function.

JavaScript supports two kinds of functions: those that are built into the language, and those you create yourself.

Special Built-in Functions
The JavaScript language includes several built-in functions. Some of them let you 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 you cannot just put directly into text. For example, the angle brackets, "<" and ">", delineate HTML tags.

The escape 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 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 function is eval(), which evaluates any valid mathematical expression that is presented in string form. The eval() function takes one argument, the expression to be evaluated.

var anExpression = "6 * 9 % 7";
var total = eval(anExpression);        // Assigns the value 5 to the variable total.
var yetAnotherExpression = "6 * (9 % 7)";
total = eval(yetAnotherExpression)        // Assigns the value 12 to the variable total.

var totality = eval("...surrounded by acres of clams.");        // Generates an error.
Consult the language reference for more information about these and other built-in functions.
Creating Your Own Functions
You can create your own functions and use them where you need them. A function definition consists of a function statement and a block of JavaScript statements.

The checkTriplet 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 checkTriplet function calls one of two other functions to make the actual test.

Notice the use of a very small number ("epsilon") as a testing 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 example determines whether it is appropriate and, if it is, uses it.

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.
    if (theCheck < 0)  {  // The test requires the absolute value, so invert theCheck if it's negative.
    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.

function checkTriplet(a, b, c)  {  // The triplet checker. First, move the longest side to position "a".
var d = 0;  // Create a temporary holding bin.
    if (c > b)  {  // If c > b, swap them.
    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.
}  // End of the triplet check function.

// The next three statements assign sample values for testing purposes.
var sideA = 5;
var sideB = 5;
var sideC = Math.sqrt(50);

checkTriplet(sideA, sideB, sideC);  // Call the function. After the call, triplet contains the result.

Home | Javascript validator | JavaScript Editor JavaScript EditorGet Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.

Если собираетесь приобрести кухню в Челябинске, заходите в магазин Мебель Всем.