JavaScript Operators
JavaScript operators are used to perform operations on variables and values. They allow you to perform calculations, assign values, and compare data.
Common JavaScript Operators
- Arithmetic: +, -, *, /
- Assignment: =, +=, -=
- Comparison: ==, ===, !=, >, <
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
Addition Operator (+)
The addition operator is used to add two values.
<!DOCTYPE html>
<html>
<body>
<p id="add"></p>
<script>
let x = 10;
let y = 5;
document.getElementById('add').innerHTML = x + y;
</script>
</body>
</html>
Try this code
Subtraction Operator (-)
The subtraction operator is used to subtract one value from another.
let x = 10;
let y = 5;
document.getElementById('sub').innerHTML = x - y;
Multiplication Operator (*)
The multiplication operator is used to multiply values.
let x = 10;
let y = 5;
document.getElementById('mul').innerHTML = x * y;
Division Operator (/)
The division operator is used to divide one value by another.
let x = 10;
let y = 5;
document.getElementById('div').innerHTML = x / y;
Assignment Operators
Assignment operators are used to assign values to variables. They can also be used to perform operations and assign the result.
Assignment Operator (=)
The assignment operator is used to assign a value to a variable.
<!DOCTYPE html>
<html>
<body>
<p id="assign"></p>
<script>
let x;
x = 10;
document.getElementById('assign').innerHTML = x;
</script>
</body>
</html>
Try this code
Addition Assignment Operator (+=)
This operator adds a value to a variable and assigns the result.
<!DOCTYPE html>
<html>
<body>
<p id="addAssign"></p>
<script>
let x = 10;
x += 5;
document.getElementById('addAssign').innerHTML = x;
</script>
</body>
</html>
Try this code
Subtraction Assignment Operator (-=)
This operator subtracts a value from a variable and assigns the result.
let x = 10;
x -= 5;
document.getElementById('subAssign').innerHTML = x;
Comparison Operators
Comparison operators are used to compare two values. They return either true or false.
Equal to (==)
This operator checks if two values are equal.
<!DOCTYPE html>
<html>
<body>
<p id="eq"></p>
<script>
let x = 10;
let y = '10';
document.getElementById('eq').innerHTML = (x == y);
</script>
</body>
</html>
Try this code
Strict Equal (===)
This operator checks both value and data type.
<!DOCTYPE html>
<html>
<body>
<p id="seq"></p>
<script>
let x = 10;
let y = '10';
document.getElementById('seq').innerHTML = (x === y)
</script>
</body>
</html>
Try this code
Not Equal (!=)
This operator checks if two values are not equal.
<!DOCTYPE html>
<html>
<body>
<p id="neq"></p>
<script>
let x = 10;
let y = 5;
document.getElementById('neq').innerHTML = (x != y);
</script>
</body>
</html>
Try this code
Greater Than (>)
Checks if one value is greater than another.
<!DOCTYPE html>
<html>
<body>
<p id="gt"></p>
<script>
let x = 10;
let y = 5;
document.getElementById('gt').innerHTML = (x > y);
</script>
</body>
</html>
Try this code
Less Than (<)
Checks if one value is less than another.
<!DOCTYPE html>
<html>
<body>
<p id="lt"></p>
<script>
let x = 5;
let y = 10;
document.getElementById('lt').innerHTML = (x < y);
</script>
</body>
</html>
Try this code
Why Operators are Important
- They allow calculations in programs
- They help in decision making
- They are used in almost every JavaScript program
Best Practices
- Use meaningful variable names
- Always understand what each operator does
- Avoid unnecessary complex expressions