An expression is a special kind of statement that evaluates to a value. Every expression consists of
- Operands: Represents the data.
- Operator: which performs certain operations on operands.
Consider the following expression - 2 / 3, in the expression, 2 and 3 are operands and the symbol /is the operator.
JavaScript supports the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Type Operators
- Miscellaneous Operators
Arithmetic Operators: As we know these are the basic mathematical operators available in JavaScript ES6.
Operator | Function |
---|
Addition(+) | Returns sum of the operands. |
---|
Subtraction(-) | Returns the difference of the values. |
---|
Multiplication(*) | Returns the product of the values. |
---|
Division(/) | Performs division and returns the quotient. |
---|
Modulus(%) | Performs division and returns the remainder. |
---|
Increment(++) | Increases the value of the variable by one. |
---|
Decrement(- -) | Decreases the value of the variable by one. |
---|
Example:
javascript
<script>
var num1 = 10.5
var num2 = 2.5
document.write("Sum : " + (num1 + num2) + "<br>");
document.write("Difference : " + (num1 - num2) + "<br>")
document.write("Product : " + num1 * num2 + "<br>")
document.write("Quotient : " + num1 / num2 + "<br>")
document.write("Remainder : " + num1 % num2 + "<br>")
// pre-increment
document.write("Value of num1 after pre-increment : "
+ (++num1) + "<br>")
// post-increment
document.write("Value of num1 after post-increment : "
+ (num1++) + "<br>")
// pre-decrement
document.write("Value of num2 after pre-decrement : "
+ (--num2) + "<br>")
// post-decrement
document.write("Value of num2 after post-decrement : "
+ (num2--) + "<br>")
</script>
Output:
Sum : 13
Difference : 8
Product : 26.25
Quotient : 4.2
Remainder : 0.5
Value of num1 after pre-increment : 11.5
Value of num1 after post-increment : 11.5
Value of num2 after pre-decrement : 1.5
Value of num2 after post-decrement : 1.5
Relational Operators: An operator that compares two values. Relational operators are sometimes called comparison operators.
Operator | Function |
---|
> | Returns true if the left operand is greater than right else, false. |
---|
< | Returns true if the left operand is lesser than right else, false. |
---|
>= | Returns true if the left operand is greater than or equal to right else, false. |
---|
<= | Returns true if the left operand is lesser than or equal to right else, false. |
---|
== | Returns true if both the operands are same else, false./td> |
---|
== | Returns true if both the operands are the same else, false. |
---|
!= | Returns true if both the operands are not same else, false. |
---|
Example:
javascript
<script>
document.write("11>12 : " + (11 > 12) + "<br>");
document.write("11<12 : " + (11 < 12) + "<br>")
document.write("12>=11 : " + (12 >= 11) + "<br>")
document.write("12<=12 : " + (12 <= 12) + "<br>")
document.write("11==11 : " + (11 == 11) + "<br>")
document.write("11!=11 : " + (11 != 11) + "<br>")
</script>
Output:
11>12 : false
11=11 : true
12<=12 : true
11==11 : true
11!=11 : false
Logical Operators: Logical operators are used to combine two or more relational statements.
Operator | Function |
---|
And(&&) | Return true if all the relational statements combined with && are true, else false. |
---|
Or(||) | Return true if at least one of the relational statements combined with || is true, else false.< |
---|
Not(!) | Returns the inverse of the relational statement’s result. |
---|
Example:
javascript
<script>
// Returns true as every statement is true.
document.write("13>12 && 12>11 && 9==9 : "
+ (13 > 12 && 12 > 11 && 9 == 9) + "<br>");
// Returns false as 11>12 is false.
document.write("11>12 && 12>11 && 9==9 : "
+ (11 > 12 && 12 > 11 && 9 == 9) + "<br>")
// As one true statement is enough to return.
document.write("11>12 || 12>11 || 9==9 : "
+ (11 > 12 || 12 > 11 || 9 == 9) + "<br>")
// Returns false as 11>12 is not true.
document.write("11>12 && (12>11 || 9==9) : "
+ (11 > 12 && (12 > 11 || 9 == 9)) + "<br>")
</script>
Output:
13>12 && 12>11 && 9==9 : true
11>12 && 12>11 && 9==9 : false
11>12 || 12>11 || 9==9 : true
11>12 && (12>11 || 9==9) : false
Bitwise Operators: A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Operator | Function |
---|
Bitwise AND(&) | Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 0, the corresponding result bit is set to 0, else 1. |
---|
Bitwise OR(|) | Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1, else 0. |
---|
Bitwise XOR(^) | Inverts the bits of its corresponding operand |
---|
Left shift(<<) | Will shift the 'n' number of bits to the left side, and n bits with the value 0 will be filled on the right side. Example: x=2, t=4, x<<t, for easy evaluation it performs x*(2^n)=2*(2^4) . |
---|
Right shift(>>) | Will shift the 'n' number of bits to the right side, and ‘n’ bits with the value 0 will be filled on the left side. Example: x=2, t=4, x>>t, for easy evaluation it performs t/(2^n)=2/(2^4) . |
---|
Zero-fill right shift | Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeroes from the left |
---|
Example:
javascript
<script>
document.write("2&3 : " + (2 & 3) + "<br>");
document.write("2|3 : " + (2 | 3) + "<br>");
document.write("2^3 : " + (2 ^ 3) + "<br>");
document.write("~4 : " + (~4) + "<br>");
document.write("2<<3 : " + (2 << 3) + "<br>");
document.write("2>>3 : " + (2 >> 3) + "<br>");
</script>
Output:
2&3 : 2
2|3 : 3
2^3 : 1
~4 : -5
2<>3 : 0
Assignment Operators: An assignment operator is the operator used to assign a new value to the variable, property, event or indexer element.
Operator | Function |
---|
Simple Assignment(=) | Assigns the value of the right operand to left operand. |
---|
Add and Assignment(+=) | It adds the right operand to the left operand and assigns the result to the left operand. |
---|
Subtract and Assignment(-=) | It subtracts the right operand from the left operand and assigns the result to the left operand. |
---|
Multiply and Assignment(*=) | It multiplies the right operand with the left operand and assigns the result to the left operand. |
---|
Divide and Assignment(/=) | It divides the left operand with the right operand and assigns the result to the left operand. |
---|
Example:
javascript
<script>
var a = 12;
var b = 10;
a = b;
document.write("a = b : " + a + "<br>");
a += b;
document.write("a += b : " + a + "<br>")
a -= b;
document.write("a -= b : " + a + "<br>")
a *= b;
document.write("a *= b : " + a + "<br>")
a /= b;
document.write("a /= b : " + a + "<br>")
</script>
Output:
a = b : 10
a += b : 20
a -= b : 10
a *= b : 100
a /= b : 10
Type Operators: It is a unary operator. This operator returns the data type of the operand.
Syntax:
typeof(operand)
Example:
javascript
<script>
var a = 12;
var b = "Geeks";
var b = "Geeks";
var c = true;
var d = String;
document.write("a is " + typeof(a) + "<br>");
document.write("b is " + typeof(b) + "<br>")
document.write("c is : " + typeof(c) + "<br>")
document.write("d is : " + typeof(d) + "<br>")
</script>
Output:
a is number
b is string
c is : boolean
d is : function
Miscellaneous Operators:These are the operators that, does different operators when used at different types of occasions.
Operator | Function |
---|
Negation operator (-) | Changes the sign of a value. |
---|
Concatenation operator (+) | +, when applied to strings it appends them. |
---|
Conditional Operator (?) | It can use as a ternary operator. |
---|
Example:
javascript
<script>
var a = "GeeksforGeeks=>";
var b = "A Computer science portal.";
var c = true;
var d = 9;
// Concatenation
document.write("a+b : " + a + b + "<br>");
// Ternary Operator
var c = 2 > 3 ? "yes is 2>3" : "No, It is not."
document.write("2>3 :" + c + "<br>")
// Negation
d = -d;
document.write("d = -d : " + d + "<br>")
</script>
Output:
a+b : GeeksforGeeks=>A Computer science portal.
2>3 :No, It is not.
d = -d : -9
Similar Reads
ES6 Spread Operator
Spread Operator is a very simple and powerful feature introduced in the ES6 standard of JavaScript, which helps us to write nicer and shorter code. The JavaScript spread operator is denoted by three dots (...). The spread operator helps the iterable objects to expand into individual elements. Iterab
3 min read
R Operators
Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R supports majorly four kinds
8 min read
Operators in LISP
Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can
5 min read
PHP Operators
In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH
8 min read
JavaScript Operators
JavaScript operators are symbols or keywords used to perform operations on values and variables. They are the building blocks of JavaScript expressions and can manipulate data in various ways.JavaScript OperatorsThere are various operators supported by JavaScript.1. JavaScript Arithmetic OperatorsAr
5 min read
SASS | Operators
SASS provides and supports a variety of useful operators in order to work with different values. These operators consist of the standard mathematical operators like +, -, /, and *, and apart from them operators of various other types listed below: +, -, *, /, and % have their usual meaning of mathem
2 min read
PHP | Bitwise Operators
The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then calculation is performed on the operands. The mathematical operations such as addition , subtraction , multiplication etc. can be performed at bit-level for faster p
5 min read
Swift - Operators
Swift is a general-purpose, multi-paradigm, and compiled programming language which is developed by Apple Inc. In Swift, an operator is a symbol that tells the compiler to perform the manipulations in the given expression. Just like other programming languages, the working of operators in Swift is a
8 min read
XPath Operators
XPath Operators are the operators which are used in XPath expression. The Operators enhance our operating ability to the XML element. Such as we can check conditions using conditional operators and check multiple conditions using Boolean operators. Types of Operators: Serial No. Operator Representat
4 min read
Bitwise Operators in LISP
In this article, we will discuss the Bitwise operators in LISP. These operators are used to perform the manipulation of individual bits of a number. The truth table for bitwise AND, NAND, OR, XOR, NOR, & XNOR: aba and b a nand ba or b a xor b a nor b a xnor b00010011010111001110100110011100 Dif
4 min read