ES6 - Operators

An expression is a special kind of statement that evaluates to a value. Every expression is composed of −
Operands − Represents the data.
Operands − Represents the data.
Operator − Defines how the operands will be processed to produce a value.
Operator − Defines how the operands will be processed to produce a value.
Consider the following expression- 2 + 3. Here in the expression, 2 and 3 are operands and the symbol + (plus) is the operator. JavaScript supports the following types of operators −
Arithmetic operators
Logical operators
Relational operators
Bitwise operators
Assignment operators
Ternary/conditional operators
String operators
Type operators
The void operator
Assume the values in variables a and b are 10 and 5 respectively.
Show Examples
Returns the sum of the operands.
Returns the difference of the values.
Returns the product of the values.
Performs a division operation and returns the quotient.
Performs a division and returns the remainder.
Increments the value of the variable by one.
Decrements the value of the variable by one.
Relational operators test or define the kind of relationship between two entities. Relational operators return a boolean value, i.e. true/false.
Assume the value of A is 10 and B is 20.
Show Examples
Logical operators are used to combine two or more conditions. Logical operators, too, return a Boolean value. Assume the value of variable A is 10 and B is 20.
Show Examples.
The operator returns true only if all the expressions specified return true.
The operator returns true if at least one of the expressions specified return true.
The operator returns the inverse of the expression’s result. For E.g.: !(7>5) returns false.
JavaScript supports the following bitwise operators. The following table summarizes JavaScript's bitwise operators.
Show Examples.
The following table summarizes Assignment operators.
Show Examples.
= (Simple Assignment)
Assigns values from the right side operand to the left side operand.
Example −  C = A + B will assign the value of A + B into C
+= (Add and Assignment)
It adds the right operand to the left operand and assigns the result to the left operand.
Example − C += A is equivalent to C = C + A
-= (Subtract and Assignment)
It subtracts the right operand from the left operand and assigns the result to the left operand.
Example C -= A is equivalent to C = C - A
*= (Multiply and Assignment)
It multiplies the right operand with the left operand and assigns the result to the left operand.
Example C *= A is equivalent to C = C * A
/= (Divide and Assignment)
It divides the left operand with the right operand and assigns the result to the left operand.
Note − The same logic applies to Bitwise operators, so they will become <<=, >>=, >>=, &=, |= and ^=.
Following are some of the miscellaneous operators.
Changes the sign of a value. The following program is an example of the same.
var x = 4 
var y = -x; 
console.log("value of x: ",x); //outputs 4 
console.log("value of y: ",y); //outputs -4
The following output is displayed on successful execution of the above program.
value of x: 4 
value of y: -4 

The + operator when applied to strings appends the second string to the first. The following program helps to understand this concept.
var msg = "hello"+"world" 
console.log(msg) 
The following output is displayed on successful execution of the above program.
helloworld

The concatenation operation doesn’t add a space between the strings. Multiple strings can be concatenated in a single statement.
This operator is used to represent a conditional expression. The conditional operator is also sometimes referred to as the ternary operator. Following is the syntax.
Test ? expr1 : expr2

Where,
Test − Refers to the conditional expression
expr1 −  Value returned if the condition is true
expr2 − Value returned if the condition is false
Example
var num = -2 
var result = num > 0 ?"positive":"non-positive" 
console.log(result)
Line 2 checks whether the value in the variable num is greater than zero. If num is set to a value greater than zero, it returns the string “positive” else a “non-positive” string is returned.
The following output is displayed on successful execution of the above program.
non-positive 

It is a unary operator. This operator returns the data type of the operand. The following table lists the data types and the values returned by the typeof operator in JavaScript.
The following example code displays the number as the output.
var num = 12 
console.log(typeof num); //output: number
The following output is displayed on successful execution of the above code.
number

ES6 provides a new operator called the spread operator. The spread operator is represented by three dots “...” . The spread operator converts an array into individual array elements.
The following example illustrates the use of spread operators in a function
<script>
   function addThreeNumbers(a,b,c){
      return a+b+c;
   }
   const arr = [10,20,30]
   console.log('sum is :',addThreeNumbers(...arr))
   console.log('sum is ',addThreeNumbers(...[1,2,3]))
</script>
The output of the above code will be as seen below −
sum is : 60
sum is 6

The spread operator can be used to copy one array into another. It can also be used to concatenate two or more arrays. This is shown in the example below −
<script>
   //copy array using spread operator
   let source_arr = [10,20,30]
   let dest_arr = [...source_arr]
   console.log(dest_arr)
	
   //concatenate two arrays
   let arr1 = [10,20,30]
   let arr2 =[40,50,60]
   let arr3 = [...arr1,...arr2]
   console.log(arr3)
</script>
The output of the above code will be as stated below  −
[10, 20, 30]
[10, 20, 30, 40, 50, 60]

The spread operator can be used to copy one object into another. It can also be used to concatenate two or more objects. This is shown in the example below −
<script>
   //copy object
   let student1 ={firstName:'Mohtashim',company:'TutorialsPoint'}
   let student2 ={...student1}
   console.log(student2)
   //concatenate objects
   let student3 = {lastName:'Mohammad'}
   let student4 = {...student1,...student3}
   console.log(student4)
</script>
The output of the above code will be as given below −
{firstName: "Mohtashim", company: "TutorialsPoint"}
{firstName: "Mohtashim", company: "TutorialsPoint", lastName: "Mohammad"}


    32 Lectures 
     3.5 hours 

 Sharad Kumar

    40 Lectures 
     5 hours 

 Richa Maheshwari

    16 Lectures 
     1 hours 

 Anadi Sharma

    50 Lectures 
     6.5 hours 

 Gowthami Swarna

    14 Lectures 
     1 hours 

 Deepti Trivedi

    31 Lectures 
     1.5 hours 

 Shweta
 Print
 Add Notes
 Bookmark this page