Introduction
In this article, we will learn about operators in JavaScript. Many programming languages use operators, and every programming language has an operator. Without further ado, let's take a look at the operators in JavaScript.
What is an Operator?
The operator is a symbol that performs some function in one or more values or functions. JavaScript operators perform math or logical functions in a variable. JavaScript supports the following types of operators:
- Arithmetic Operators
- Relational or comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional or ternary Operators
Arithmetic Operators
These operators are used to perform arithmetic operations. Arithmetic operators are of two types:
- Unary Operators – It will operate a single operand
- Binary Operators – It will operate two operands
Arithmetic Operators
|
Description
|
Binary
|
Operators
|
+
|
It performs an addition operation
|
-
|
It performs a subtraction operation
|
*
|
It performs a multiplication operation
|
/
|
It performs a division operation
|
%
|
It calculates the remainder after division
|
Unary
|
Operators
|
++
|
It performs an increment on a given number
|
--
|
It performs a decrement on a given number
|
Example of Binary Operators
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Arithmetic Operators</title>
- </head>
- <body>
- <h2>Arithmetic Operators in JavaScript</h2>
- <script type="text/javascript">
- var a=10;
- var b=20;
- document.write("The sum of two numbers a=10, b=20 <br>");
- document.write("Addition :"+(a+b));
- document.write("<br>");
- document.write("Subraction :"+(a-b));
- document.write("<br>");
- document.write("Multiplication :"+(a*b));
- document.write("<br>");
- document.write("Division :"+(a/b));
- document.write("<br>");
- document.write("Modules :"+(a%b));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
Example of Unary Operators
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Urinary Operators</title>
- </head>
- <body>
- <h2>Urinary Operators in JavaScript</h2>
- <script type="text/javascript">
- var a=20;
- var b=30;
- document.write("The sum of two numbers a=20, b=30 <br>");
-
- document.write("Post increment 20++ :"+(a++));
- document.write("<br>");
- document.write("After increment the value of a is :"+(a));
- document.write("<br>");
-
- document.write("Pre increment ++30 :"+(++b));
- document.write("<br>");
- document.write("After increment the value of b is :"+(b));
- document.write("<br> <br>");
-
- document.write("Decrement Operators <br>");
- document.write("Post decrement 20-- :"+(a--));
- document.write("<br>");
- document.write("After decrement the value of a is :"+(a));
- document.write("<br>");
-
- document.write("Pre decrement --30 :"+(--b));
- document.write("<br>");
- document.write("After decrement the value of b is :"+(b));
- </script>
- </body>
- </html>
Output
String using ‘+’ Operator
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <title>String Operator</title>
- </head>
- <body>
- <h2>String Operator in JavaScript</h2>
- <script type="text/javascript">
- var str = "Hi";
- var str1 = "C sharp cornner!";
- document.write(str+str1);
- </script>
- </body>
- </html>
Output
Relational or Comparison Operators
These operators are used to compare two values or variables. They are used to find the relationship between two values or compare the relationship between two values that yield the result true or false.
Comparison operator
|
Description
|
==
|
Equal to
|
===
|
Equal value and Equal type
|
>=
|
Greater than equal to
|
<=
|
Less than equal to
|
>
|
Greater than
|
<
|
Less than
|
!=
|
Not equal to
|
!==
|
Not equal value and not equal type
|
Example of a comparison operator
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Comparison Operator</title>
- </head>
- <body>
- <h2>Relational or Comparison Operators</h2>
- <script type="text/javascript">
- var a=5;
- var b=7;
- document.write(a==b);
- document.write("<br>");
- document.write(a===b);
- document.write("<br>");
- document.write(a>=b);
- document.write("<br>");
- document.write(a<=b);
- document.write("<br>");
- document.write(a>b);
- document.write("<br>");
- document.write(a<b);
- document.write("<br>");
- document.write(a!=b);
- document.write("<br>");
- document.write(a!==b);
- document.write("<br>");
- </script>
- </body>
- </html>
Output
Logical Operator
These operators are used to perform Boolean expressions. This is true or false. This makes a logical AND, OR, and NOT. It is often used for conditional statements.
Logical Operator
|
Description
|
&&
|
It performs logical AND operation
|
||
|
It performs logical OR operation
|
!
|
It performs logical Not operation
|
Example of a logical operator
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Logical Operator</title>
- <script type="text/javascript">
- function verify() {
- var name = document.getElementById("nam").value;
- var pass = document.getElementById("pwd").value;
- if ((name=="vijay") && (pass=="logical"))
- {
- document.write("Login successfully!");
- }
- else
- {
- document.write("Login failed");
-
- }
- }
- </script>
- </head>
- <body>
- <h2>Logigal operator in JavaScript</h2>
- <h3>Login Id</h3>
- User Name<input type="text" name="name" id="nam" placeholder="Enter your Name">
- <br><br>
- Password<input type="Password" name="pwd" id="pwd" placeholder="Enter your Password">
- <br><br>
- <input type="submit" value="submit" onclick="verify();">
- </body>
- </html>
Output
Bitwise Operators
JavaScript bitwise operators are used to perform functions on their bits. Bit means (0 and 1s) or true or false. The sequence of 8 bits is called byte. The bitwise operator converts a given decimal number into a binary number.
Bitwise operators
|
Description
|
&
|
Performs AND operation on the bits of operands
|
|
|
Performs OR operation on the bits of operands
|
^
|
Performs XOR operation on the bits of operands
|
~
|
Performs NOT operation on the bits of operands
|
<<
|
Left shift is multiplication
|
>>
|
Right Shift is division
|
>>>
|
Right Shift with zero
|
Example of a Bitwise operator
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bitwise Operator</title>
- </head>
- <body>
- <h2>Bitwise Operator in JavaScript</h2>
- <script type="text/javascript">
- document.write("AND Operator (1 & 1) is :"+(1 & 1));
- document.write("<br>");
- document.write("AND Operator (1 & 0) is :"+(1 & 0));
- document.write("<br>");
- document.write("OR Operator (0 | 0) is :"+(0 | 0));
- document.write("<br>");
- document.write("OR Operator (1 | 1) is :"+(1 | 1));
- document.write("<br>");
- document.write("XOR Operator (1 ^ 1) is :"+(1 ^ 1));
- document.write("<br>");
- document.write("XOR Operator (0 ^ 0) is :"+(0 ^ 0));
- document.write("<br>");
- document.write("XOR Operator (1 ^ 0) is :"+(1 ^ 0));
- document.write("<br>");
- document.write("XOR Operator (0 ^ 1) is :"+(0 ^ 1));
- document.write("<br>");
- document.write("Left Shift (3<<5) is :"+(3 << 5));
- document.write("<br>");
- document.write("Right Shift (18 >> 4) is :"+(18 >> 4));
- document.write("<br>");
- document.write("NOT Operator (~5) is :"+(~5));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
Assignment Operator
An assignment operator is one of the simplest operators used to assign a value to variable or constant. The right side could be a variable or constant, the left side of the operator must be a variable.
Assignment Operator
|
Description
|
=
|
Assigns the right-side value of the expression to the left side value of the expression.
|
+=
|
Add the variables present on either side of the expression
|
-=
|
Subtract the variables present on either side of the expression
|
*=
|
Multiplication of the variables present on either side of the expression
|
/=
|
Divide the variables present on either side of the expression
|
%=
|
Modulus operation on the variables present on either side of the expression.
|
Example of an assignment operators
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Assignment Operator</title>
- </head>
- <body>
- <h2>Assignment operator in JavaScript</h2>
- <script type="text/javascript">
- var a=10;
- document.write("The value of a =",a);
- document.write("<br>");
- a=20;
- document.write("The value of a =",a);
- document.write("<br>");
- a+=20;
- document.write("The value of a+ =",a);
- document.write("<br>");
- a-=10;
- document.write("The value of a- =",a);
- document.write("<br>");
- a*=10;
- document.write("The value of a* =",a);
- document.write("<br>");
- a/=10;
- document.write("The value of a/ =",a);
- document.write("<br>");
- a%=10;
- document.write("The value of a% =",a);
- document.write("<br>");
-
- </script>
- </body>
- </html>
Output
Conditional Operators
Conditional operators are used to perform some conditional checks on expressions. The operator operates three expressions and contains symbols. Conditional operators also called ternary operators.
Syntax
<condition>? <value1>: <value 2>
Try it yourself:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Conditonal Operator </title>
- </head>
- <body>
- <h2>Conditional or ternary operator in JavaScript</h2>
- <h2>Result</h2>
- Enter Name
- <input type="text" name="name1" id="n1">
- <br><br>
- Enter Mark
- <input type="number" name="mark" id="mrk">
- <br><br>
- <input type="submit" value="check" onclick="result();">
- <script type="text/javascript">
- function result() {
- var name = document.getElementById("n1").value;
- var mark = document.getElementById("mrk").value;
- (mark>=35)?document.write(name+"pass mark"):document.write(name+"Fail mark");
- }
- </script>
- </body>
- </html>
Output
Try it yourself:
Try it your self,
Summary
This article, we have learned about Operators in JavaScript. I hope this article is useful for you. If you have any comments leaving a comment. Thank you!