Page 47 - Trackpad_V2.1_class8
P. 47
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 mathematical calculations, you need to convert the value into an integer
by using the parseInt( ) method and into a floating-point number by using the parseFloat( )
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("The sum 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:
Dynamic Web Pages in HTML5 45

