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.

Home Page - Basic Engineer
Home Page – Basic Engineer

There are following types of operators in JavaScript

  1. Arithmetic Operators.
  2. Assignment Operators.
  3. Comparison Operators.
  4. Logical Operators.
  5. Ternary Operators.

JavaScript Arithmetic Operators

OperatorDescription
+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

OperatorDescription
=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

OperatorDescription
==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.


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *