- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Operators
JavaScript Basics
JavaScript Operators
JavaScript operators are symbols used to perform operations on values and variables.
Operators can be used for addition, subtraction, comparison, assignment, logical checks, and more.
Example
Example
javascript
let a = 10;
let b = 5;
let result = a + b;Here, the + operator adds two values.
Why Operators are Used
Operators help JavaScript perform different actions.
For example:
Example
javascript
let total = 100 + 50;
let price = 500 - 100;
let result = 10 > 5;In the above code:
| Code | Meaning |
|---|---|
100 + 50 | Adds two numbers |
500 - 100 | Subtracts one number from another |
10 > 5 | Checks if 10 is greater than 5 |
Operators are used in almost every JavaScript program.
Types of JavaScript Operators
JavaScript has different types of operators.
| Operator Type | Use |
|---|---|
| Arithmetic Operators | Perform mathematical calculations |
| Assignment Operators | Assign values to variables |
| Comparison Operators | Compare two values |
| Logical Operators | Check multiple conditions |
| String Operators | Join strings |
| Increment and Decrement Operators | Increase or decrease values |
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Name | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
** | Exponentiation | a ** b |
Example
javascript
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);The % operator returns the remainder after division.
Example
javascript
let result = 10 % 3;Here, the result is 1 because 10 divided by 3 leaves a remainder of 1.
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example | Same As |
|---|---|---|
= | x = 10 | x = 10 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
Example
javascript
let x = 10;
x += 5;
console.log(x);Here, x += 5 means x = x + 5.
Comparison Operators
Comparison operators are used to compare two values.
They return either true or false.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 10 == "10" |
=== | Equal value and equal type | 10 === 10 |
!= | Not equal | 10 != 5 |
!== | Not equal value or not equal type | 10 !== "10" |
> | Greater than | 10 > 5 |
< | Less than | 5 < 10 |
>= | Greater than or equal to | 10 >= 10 |
<= | Less than or equal to | 5 <= 10 |
Example
javascript
let a = 10;
let b = 5;
console.log(a > b);
console.log(a < b);
console.log(a === b);Use === instead of == in most cases because it checks both value and data type.
Example
javascript
console.log(10 == "10");
console.log(10 === "10");== only compares values, but === compares value and type.
Logical Operators
Logical operators are used to check more than one condition.
| Operator | Name | Meaning |
|---|---|---|
&& | AND | Returns true if both conditions are true |
|| | OR | Returns true if at least one condition is true |
! | NOT | Reverses the result |
Example
javascript
let age = 20;
let hasId = true;
console.log(age >= 18 && hasId);Here, both conditions must be true.
Example with OR:
Example
javascript
let isEmailVerified = false;
let isPhoneVerified = true;
console.log(isEmailVerified || isPhoneVerified);Here, the result is true if at least one condition is true.
Example with NOT:
Example
javascript
let isLoggedIn = false;
console.log(!isLoggedIn);The ! operator reverses false into true.
String Operator
The + operator can also be used to join strings.
Example
javascript
let firstName = "Amit";
let lastName = "Kumar";
let fullName = firstName + " " + lastName;Here, the + operator joins two strings.
You can also join strings with numbers.
Example
javascript
let message = "Age: " + 25;JavaScript converts the number into a string and joins it with the text.
Increment Operator
The increment operator ++ increases a value by 1.
Example
javascript
let count = 5;
count++;
console.log(count);Here, the value of count becomes 6.
Decrement Operator
The decrement operator -- decreases a value by 1.
Example
javascript
let count = 5;
count--;
console.log(count);Here, the value of count becomes 4.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Operators</title>
</head>
<body>
<h1>JavaScript Operators</h1>
<p id="result"></p>
<script>
let a = 20;
let b = 10;
let addition = a + b;
let subtraction = a - b;
let multiplication = a * b;
let division = a / b;
let isGreater = a > b;
let bothTrue = a > b && b > 5;
document.getElementById("result").innerHTML =
"Addition: " + addition + "<br>" +
"Subtraction: " + subtraction + "<br>" +
"Multiplication: " + multiplication + "<br>" +
"Division: " + division + "<br>" +
"Is A greater than B? " + isGreater + "<br>" +
"Both conditions true? " + bothTrue;
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try changing the values of a and b.
Also try using different operators like %, ===, ||, ++, and --.
Important Points
- Operators are used to perform operations on values and variables.
- Arithmetic operators are used for mathematical calculations.
- Assignment operators are used to assign or update values.
- Comparison operators return
trueorfalse. - Logical operators are used to check multiple conditions.
- The
+operator can add numbers and also join strings. - Use
===for strict comparison in JavaScript.
Conclusion
JavaScript operators are used to perform calculations, assign values, compare values, join strings, and check conditions.
Operators are a basic but very important part of JavaScript. Once you understand operators, you can write better expressions, conditions, calculations, and interactive programs.
