Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 74 - 2625 PortGPt V2.1
P. 74

    When the preceding statement is executed, a prompt box will appear in the web browser asking
                      to input the age. By default, the input taken by the prompt( ) method is considered as a string in
                      JavaScript.
                      If you want to perform some mathematical calculations, then we need to convert the value into an
                      integer by using the parseInt( ) method. We can use the parseInt( ) method in the following way:

                      var num = parseInt(prompt("Enter first number: "));
                      The document.write( ) method is used to display output on the web page.

                      For example:

                      document.write("Hello from JavaScript");
                      The preceding statement will display the message "Hello from JavaScript" on the web page. Let us
                      create a web page to calculate the sum of two numbers in JavaScript.

                      <HTML>

                      <HEAD>
                      <TITLE> Statements in JavaScript </TITLE>
                      </HEAD>
                      <BODY>
                      <SCRIPT type="text/javascript">
                      var a = parseInt(prompt("Enter first number: "));
                      var b = parseInt(prompt("Enter second number: "));

                      var c;
                      c = a + b;
                      document.write("Sum of a and b is: ");
                      document.write(c);
                      </SCRIPT>

                      </BODY>
                      </HTML>
                      Save the file with .html extension and open the web page in web browser. You will get the following
                      output:

                      72    Premium Edition-VIII

| 69 70 71 72 73 74 75 76 77 78 79 |