Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition,
subtraction, multiplication, and division.
Example Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operators</title>
</head>
<body>
<h3>Arithmetic Operators Example</h3>
<script>
let a = 10;
let b = 3;
let addition = a + b;
let subtraction = a - b;
let multiplication = a * b;
let division = a / b;
let modulus = a % b;
document.write("Addition (10 + 3) = " + addition + "<br>");
document.write("Subtraction (10 - 3) = " + subtraction + "<br>");
document.write("Multiplication (10 * 3) = " + multiplication + "<br>");
document.write("Division (10 / 3) = " + division + "<br>");
document.write("Modulus (10 % 3) = " + modulus + "<br>");
</script>
</body>
</html>
2. Comparison Operators
Comparison operators compare two values and return a boolean (true or false).
Example Operators:
== (Equal to)
!= (Not equal to)
=== (Strict equal to)
!== (Strict not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
<!DOCTYPE html>
<html>
<head>
<title>Comparison Operators</title>
</head>
<body>
<h3>Comparison Operators Example</h3>
<script>
let x = 5;
let y = "5";
let result1 = (x == y); // true, because values are equal
let result2 = (x === y); // false, because types are different
let result3 = (x != y); // false, because values are equal
let result4 = (x !== y); // true, because types are different
let result5 = (x > 3); // true
let result6 = (x < 10); // true
document.write("5 == '5': " + result1 + "<br>");
document.write("5 === '5': " + result2 + "<br>");
document.write("5 != '5': " + result3 + "<br>");
document.write("5 !== '5': " + result4 + "<br>");
document.write("5 > 3: " + result5 + "<br>");
document.write("5 < 10: " + result6 + "<br>");
</script>
</body>
</html>
3. Logical Operators
Logical operators are used to combine two or more conditions.
Example Operators:
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
<!DOCTYPE html>
<html>
<head>
<title>Logical Operators</title>
</head>
<body>
<h3>Logical Operators Example</h3>
<script>
let a = true;
let b = false;
let andResult = a && b; // false, because both are not true
let orResult = a || b; // true, because at least one is true
let notResult = !a; // false, because a is true
document.write("true && false: " + andResult + "<br>");
document.write("true || false: " + orResult + "<br>");
document.write("!true: " + notResult + "<br>");
</script>
</body>
</html>
4. Assignment Operators
Assignment operators are used to assign values to variables.
Example Operators:
= (Assign)
+= (Add and assign)
-= (Subtract and assign)
*= (Multiply and assign)
/= (Divide and assign)
%= (Modulus and assign)
5. Increment and Decrement Operators
These operators are used to increase or decrease a variable's value by 1.
Example Operators:
++ (Increment)
-- (Decrement)
<!DOCTYPE html>
<html>
<head>
<title>Increment and Decrement Operators</title>
</head>
<body>
<h3>Increment and Decrement Operators Example</h3>
<script>
let count = 5;
count++; // Increment by 1
let incrementResult = count; // 6
count--; // Decrement by 1
let decrementResult = count; // 5
document.write("After increment: " + incrementResult + "<br>");
document.write("After decrement: " + decrementResult + "<br>");
</script>
</body>
</html>
String Operators
The + operator can also be used to concatenate (join) strings.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<title>String Operators</title>
</head>
<body>
<h3>String Operators Example</h3>
<script>
let greeting = "Hello, ";
let name = "Alice";
let message = greeting + name; // Concatenates the two strings
document.write("Greeting message: " + message + "<br>");
</script>
</body>
</html>