Page 65 - Chinmaya_C8_flipbook
P. 65
OPERATORS
An operator is a symbol that is used to perform calculations on values or variables. The variables
or values on which the operator performs calculations are called operands. Some of the examples
of operators are + (Addition), - (Subtraction), * (Multiplication), / (Division), etc.
EXPRESSIONS
An expression is a unit of code that is evaluated to a value. It is a combination of values and
operators. For example,
a = b + c;
COMMENTS
A comment is used to add notes in your code or to disable sections of code without deleting
them. Comments are ignored by the JavaScript interpreter. There are two types of comments in
JavaScript: single line and multi line. Single-line comments are added by using // and multi-line
comments are added between /* and */.
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 the 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.
Dynamic Web Pages in HTML5 63

