Page 148 - ComputerGenius_V2.1_Class8
P. 148

For example:

                  // alert("Hello");  Single line comment

                  /* a = 10;

                      b = 20;                  Multi line comment
                  */

                  Input and Output in JavaScript


                  JavaScript allows us to take input from the user with the help of prompt( ) method. We can use the
                  prompt( ) method in the following way:

                  var age = prompt("Enter your age: ");

                  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>



                      146    Computer Genius (V2.1)-VIII
   143   144   145   146   147   148   149   150   151   152   153