Page 295 - Web Applications (803) Class 11
P. 295
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 by using /* and */.
For example:
// alert("Hello"); Single line comment
/* a = 10;
b = 20; Multiline comment
*/
Operators
Operators are special symbols that are used to perform mathematical, logical, and relational operations on values and
variables. The variables or values on which the operators work are called operands. Some commonly used operators in
JavaScript are arithmetic operators, assignment operators or shorthand assignment operators, comparison operators,
logical operators, etc.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numbers in JavaScript. JavaScript provides the
following arithmetic operators:
Operator Name Description Example Output
(a = 11, b = 4)
+ Addition Adds two operands. a + b 15
– Subtraction Subtracts the operand written on the right-hand side of the a – b 7
operator from the operand written on the left side.
* Multiplication Multiplies both operands. a * b 44
/ Division Divides numerator by denominator. a / b 2.75
% Modulus Divides numerator by denominator and returns the remainder. a % b 3
** Exponent Calculates the base to the exponent power, that is base^ b**2 16
exponent
++ Increment Used to increase the value of a variable by 1. ++a 12
-- Decrement Used to decrease the value of a variable by 1. --a 10
Example: To calculate the square and cube of two numbers using JavaScript arithmetic operators.
<HTML>
<BODY>
<SCRIPT>
var num=5;
var sq=num**2;
var cube=num**3;
document.writeln("Square of "+num+"="+sq);
document.write("<br> <br>");
document.write("Cube of "+num+"="+cube);
</SCRIPT>
Introduction to Dynamic Websites Using JavaScript 293

