Operators are use to performs mathematical and logical computations on operands. In JavaScript operators are use for compare values, perform arithmetic operations, Ternary Operators etc. There are various operators supported by JavaScript is knows as JavaScript Operators.
There are following types of operators in JavaScript
- Arithmetic Operators.
- Assignment Operators.
- Comparison Operators.
- Logical Operators.
- Ternary Operators.
JavaScript Arithmetic Operators
Operator | Description |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
++ | Increment |
— | Decrement |
** | Exponentiation |
Addition
<html>
<body>
<p id="sum"></p>
<script>
let x = 56 + 52;
document.getElementById("sum").innerHTML = x;
</script>
</body>
</html>
Output : 108
Subtraction
<html>
<body>
<p id="Subtraction"></p>
<script>
let x = 56 - 52;
document.getElementById("Subtraction").innerHTML = x;
</script>
</body>
</html>
Output : 4
Multiplication
<html>
<body>
<p id="Multiplication"></p>
<script>
let x = 4*2;
document.getElementById("Multiplcation").innerHTML = x;
</script>
</body>
</html>
Ontput : 8
Division
<html>
<body>
<p id="Division"></p>
<script>
let x = 4/2;
document.getElementById("Multiplcation").innerHTML = x;
</script>
</body>
</html>
Output : 2
JavaScript Assignment Operators
Operator | Description |
= | Assignment Operators. |
+= | Add and Assignment Operators. |
-= | Subtract and Assignment Operators. |
*= | Multiply and Assignment Operators. |
%= | Modulus and Assignment Operators. |
Add and Assignment Operators
<html>
<body>
<p id="sum"></p>
<script>
let x = 10;
x += 5;
document.getElementById("sum").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Output : 15
Subtract and Assignment Operators
<html>
<body>
<p id="two"></p>
<script>
let x = 10;
x -= 5;
document.getElementById("two").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Output : 5
JavaScript Comparison Operators
Operator | Description |
== | equal to |
=== | equal value and type |
!= | Not equal to |
!== | Not equal value and Not type |
> | greater than |
< | less than |
>= | greater than and equal to |
<= | less than and equal to |
JavaScript ternary Operator
Evaluates a condition and executes a block of code bases on the condition is knows as JavaScript ternary Operator.
Conditional Statement
Sometimes we might have to execute a block of code based off some condition
In JavaScript we have three forms of if…. else statement
- if statement
- if else statement
- if else if else statement
If statement
The if statement evaluates the condition inside the () if the condition is evaluates to true, the code inside the body of if is executes else the code is not execute.
Syntax
If (condition){
// execute this code
}
If- else Statement
The if statement can have an optional else clause. If the condition is true, code inside if is executed else code inside else block is executes.
Syntax
if (condition){
// block of code if condition true
}
else{
//block of code if condition false
}
if- else if statement
Sometimes we might want to keep rechecking a set of conditions one by one until one matches. we use if else if for achieving this.
Syntax
if (age>0){
console.log("A valid age");
}
else if (age>10 && age<15){
console.log ("but you are a kid");
}
else if (age>18){
console.log("not a kid");
}
else{
console.log("Invalid Age")
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JS please check Wikipedia click here
Stay Connected Stay Safe. Thank you.
0 Comments