Latest posts

10/recent/ticker-posts

Operators in Javascript | JavaScript Operators

 Operators in JavaScript

In programming languages we need to use the operators to run and wxecute the programs properly.

What is Opeartors ?

Operators are the symbols that can be used to perform the specific operation on variables.
Such as assigning values to variables, Basic arithmetic operations, defining iterations of loops,etc.

There are Different types of Operators are available in Javascript to perform the operations.

Types of Operators :

  1. Assignment Operators
  2. Arithmetic Operators
  3. Comparison Operators
  4. Logical Operators
  5. Conditional Operators
  6. Type Operators

1) Assignment Operator

An assignment operator (=) are used to Assign the values to the Variables.
The basic use of this operator is assigning the values to the variables.
In that the most comon assignement operator is equal sign (=), which is used to assign the values to the variables.

Example :
 var x = 10;
This code assigns the Value 10 to variable x.

In addition to the equal sign, there are also several shorthand assignment operators that can be used to perform common operations and then assign the result to a variable.
These include the +=, -=, *=, and /= operators, which add, subtract, multiply, and divide a value by the value of a variable, respectively.

Assignment Operators in Javascript

Operator Name
= Assignment
+= Add and Assign
-= Subtract and Assign
*= Multiply and Assign
/= Divide and Assign

To assign the values to variables, we uses equal sign (=)
 var x = 10;
Add and Assign operator used to add any other value in the value already present in variable.
 var x = 10;
 x += 5;

 // Output is 15

Subtract and Assign operator used to subtract any other value from the value present in variable.
 var x = 10;
 x -= 5;

 // Output is 5

Multiply and Assign operator is used to multiply the value present in variable with any another value.
 var x = 10;
 x *= 5;

 // Output is 50
Divide and Assign operator is used to divide the value present in variable with any another value.
 var x = 10;
 x /= 5;

 // Output is 2

Example :
<script>
  function add(a, b)
  {
    a += b;
    document.getElementById("add").innerHTML = a;
  }

  function sub(a, b)
  {
    a -= b;
    document.getElementById("sub").innerHTML = a;
  }

  function mul(a, b)
  {
    a *= b;
    document.getElementById("mul").innerHTML = a;
  }

  function div(a, b)
  {
    a /= b;
    document.getElementById("div").innerHTML = a;
  }
</script>


Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on Variables or Numbers.
The most common arithmetic operators are Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%).

Arithmetic Operators in Javascript

Operator Name
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

Example :
 <script>
   var a = 10;
   var b = 5;

   document.getElementById("add").innerHTML = a + b;

   document.getElementById("sub").innerHTML = a - b;

   document.getElementById("mul").innerHTML = a * b;

   document.getElementById("div").innerHTML = a / b;

   document.getElementById("mod").innerHTML = a % b;

 </script>


Comparision Operators

Comparision operators are used to compare the two or more than two values of variables and determine whether they are equal, greater than, less than, or not equal to each other.
It returns true or false at the time of comparision.

Comparision Operators in Javascript

Operator Name
== equal to
!= not equal to
=== equal value and type
!== not equal value and type
< Less than
> Greater than
>= Greater than equal to
<= Less than equal to

Example :
 <script>
   var a = 10;
   var b = 5;

   document.getElementById("equal").innerHTML = a == b;
   document.getElementById("nequal").innerHTML = a != b;
   document.getElementById("less").innerHTML = a < b;
   document.getElementById("big").innerHTML = a > b;
   document.getElementById("Lequal").innerHTML = (a <= b);
   document.getElementById("Gequal").innerHTML = (a >= b);
   document.getElementById("Eequal").innerHTML = (a === b);
   document.getElementById("Nequal").innerHTML = (a !== b);
 </script>

Logical Operators

Logical Operators are used to evaluate the logical expressions.
Suppose you have more than two conditions to check, and you want to chack the differ between more than two condition.
At that time we can able to use the Logical Operators.

Logical Operators in Javascript

Operator Name
&& Logical AND
|| Logical OR
! Logical NOT

Example :
 <script>
 var a = 10;
 var b = 5;
 var c = 9;

 if(a > b && a > c)
 {
   document.getElementById("result").innerHTML = a;
 }
 else if(b > a && b > c)
 {
   document.getElementById("result").innerHTML = b;
 }
 else
 {
   document.getElementById("result").innerHTML = c;
 }
 </script>


Conditional Operators or Ternary Operator

It is an Another type of operators, which is quite similar as if - else statement.
It is an shorten way to write an if else statement.
It takes three operands, first is condition, if the condition is true second operand is value, but if the value is false, then the third operand is value.
 condition ? <expression if true> : <expression if false>

Example :
 let x = 10;
 let result = x > 5 ? 'Greater than 5' : 'Less than or equal to 5';
 console.log(result)


Type Operator

Lastly, the typeof operator is used to determine the type of a variable or value.
The typeof operator returns a string that represents the type of the operand.

Suppose we created a variable, which can holds a string value 'Hello World'.
When we uses the typeof operator with those variable, then it will shows the result like 'string'.


  <div class="example1">
    <br>
  <p>Hello World is a <u><span id="result"></span></u> vlaue !</p>
  </div>

  <script>
  var x = "Hello World !"
  document.getElementById("result").innerHTML = typeof(x);
  </script>





Also Visit


Post a Comment

0 Comments