How to Add Two Numbers With JavaScript
JavaScript is a client-side scripting language used to provide interactivity to web pages. A client-side scripting language uses the client computer, not the server, to execute the script. JavaScript uses variables to hold a value. When this value is a number, the addition symbol (+) is used to add two variables together.
Instructions
-
-
1
Open a blank text file.
-
2
Type the line "<SCRIPT LANGUAGE="JavaScript"> to start the script.
-
-
3
Type the line "var Num1, Num2;" to create two variables to hold the numbers.
-
4
Type the line "Num1 = 10;" to assign the number 10 to the Num1 variable.
-
5
Type the line "Num2 = 20;" to assign the number 20 to the Num2 variable.
-
6
Type the line "document.write("Number 1 is ");" to write the line "Number 1 is" to the screen.
-
7
Type the line "document.write (Num1);" to write the value of the Num1 variable (10) to the screen.
-
8
Type the line "document.write ("");" to end the first line.
-
9
Type the line "document.write ("Number 2 is ");" to write the line "Number 2 is" to the screen.
-
10
Type the line "document.write (Num2);" to write the value of the Num2 variable (20) to the screen.
-
11
Type the line "document.write ("");" to end the second line.
-
12
Type the line "document.write ("The sum is ");" to write the line "The sum is" to the screen.
-
13
Type the line "document.write (Num1 + Num2);" to write the sum of Num1 and Num2 to the screen.
-
14
Type the line "</SCRIPT>" to end the script.
-
15
Save and close the file.
-
1