// these three methods are built in into // the JavaScript String object; // they can be called on any string; // there are three event handlers in the form // that call a string method; // the if (...) statement below checks the // name of the field from where the call // was made and associates the name with one method. // we could have written three if (...) statements // (without the else if (...) statements ) // since we have three unique conditions that // are mapped to a single method, // but in that case every function call would // test all every if (...) statement in the // function; this way a function call moves // through the if (...) else if (...) block // until it finds a match and returns; function doStringFunction(myString, field) { if (field.name == "showStringLength") { field.value = myString.length; } else if (field.name == "showUpperCase") { field.value = myString.toUpperCase(); } else if (field.name == "showLowerCase") { field.value = myString.toLowerCase(); } } // Note: for short assignment statements like these examples // we didn't need to write a function at all; we could have // made each assignment in the event handler statement; // This is only an example without any claims on efficiency. // However, it is a good idea to set up functions if calls are // made frequently, even if the action performed seems trivial. // It saves coding and puts important pieces of code in places // that are easy to find and maintain.