JavaScript Functions


To keep the browser from executing a script as soon as the page is loaded, you can write your script as a function. Functions are defined at the beginning of a page, in the <head> section.

The script below uses two methods of the object String to check the alphabetic name:
 function name( var1, var2, ..., varX ) {
   some code
 }

  • The method indexOf(value [,from]) returns the index within the calling String object of the first occurrence of the specified value, starting the search at from, or -1 if the value is not found.

  • The method charAt returns the specified character from the string.
There are two kinds of variables:



   

Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     





      After learning to drive a stick shift,    
      driving with an automatic transmission is child’s play (a very easy task).