javascript operators

Operators are used to perform operations on variables and values. Operators can be divided into the following groups.

Assignment operators

Assignment operators are used to define a value to a variable. The basic assignment operator in javascript is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as... Description
x = y x = y The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus

Use of Assignment Operators

<script>
x = webtricks;
y = home;
document.write(x);
</script>

Result: 21

Arithmetic operators

The arithmetic operators are used to perform arithmetical operations on data such as addition, substraction, multiplication, etc.

Operator Name Example Result
+ Addition x + y Sum of x and y
- Subtraction x - y Difference of x and y
* Multiplication x * y Product of x and y
/ Division x / y Quotient of x and y
% Modulus x % y Remainder of x divided by y
** Exponentiation x ** y Result of raising x to the power of y

Use of Arithmetic Operators

<script>
x = 2;
y = 8;
document.write(x**y);
</script>

Result: 256

Comparison operators

Comparison operators are useful when you need to compare two values. It returns boolean value true or false.

Operator Name Example Result
== Equal x == y Returns true if x is equal to y
=== Identical x === y Returns true if x is equal to y, and both of them are string or numeric value
!= Not equal x != y Returns true if x is not equal to y
!== Not identical x !== y Returns true if x is not equal to y, or they are not of the same type
> Greater than x > y Returns true if x is greater than y
< Less than x < y Returns true if x is less than y
>= Greater than or equal to x >= y Returns true if x is greater than or equal to y
<= Less than or equal to x <= y Returns true if x is less than or equal to y

Use of Comparison Operators

<script>
x = 3;
y = 3;
if(x > y){
  document.write('x is greater');
}else if(x < y){
  document.write('y is greater');
}else{
  document.write('Both are equal');
}
</script>

Increment/Decrement operators

The increment operators are used to increase a variable's value by 1 while the decrement operators are used to decrease a variable's value by 1.

Operator Name Description
++x Pre-increment Increases the value of x by 1 and returns x
x++ Post-increment Returns x then increases the value of x by 1
--x Pre-decrement Decreases the value of x by 1, then returns x
x-- Post-decrement Returns x, then decreases the value of x by 1

Use of arithmetic operators

<script>
x = 2;
document.write(x++);
document.write(x);
</script>

The first script will print the value of x as 2 and then increases its value. The second variable x receives the increased value so prints it as 3.

Logical operators

The logical operators are used to combine conditional statements.

Operator Name Example Result
&& And x && y True if both x and y are true
|| Or x || y True if either x or y is true
! Not !(x) True if x is not true

Use of Logical Operators

<script>
x = 2;
y = 3;
if(x > 2 && y > 2){
  document.write("Both are greater than 2");
}else{
  document.write("Either one or both are not greater than 2");
}
</script>

0 Like 0 Dislike 0 Comment Share

Leave a comment