ES6 operatorsThe operator can be defined as a symbol that tells the system to implement a particular operation. In JavaScript, there is a rich set of operators, and by using specific operators, you can perform any particular task. The operators are used in the expressions for evaluating different operands. An expression is a kind of statement that returns a value. The expression includes:
For example: Suppose an expression like x*y. In this expression, x and y are the operands, and the asterisk (*) symbol is the multiplication operator. Types of OperatorsOperators in JavaScript can be classified as:
Let us try to elaborate on these operators in detail. Arithmetic OperatorsArithmetic operators are the basic mathematical operators that are available in JavaScript ES6. These operators are responsible for performing all mathematical operations such as addition, subtraction, etc. in JavaScript.
For example In this example, we are using all arithmetic operators that are listed above: Output: When you execute the above code in the terminal, you will get the following output: Addition : 50 Subtraction: 10 Multiplication: 600 The division will give you the quotient: 1.5 Modulus will give you the Remainder: 10 Value of x after preincrement: 31 Value of x after postincrement: 31 Value of y after predecrement: 19 Value of y after postdecrement: 19 Relational OperatorsRelational operators are used for comparing the two values and return either true or false based on the expression. These operators are sometimes called Comparison Operators.
For example: In this example, we are using all relational operators that are listed above Output: When you execute this code in the terminal, you will get the following output: Value of x: 20 Value of y: 15 x is greater than y: true x is smaller than y: false x is greater than or equal to y: true x is smaller than or equal to y: false x is equal to y: false x not equal to y: true Logical OperatorsLogical operators are generally used for combining two or more relational statements. They also return Boolean values.
For example: In this example, we are using all logical operators that are listed above. Output: Value of x = 30 Value of y = 80 (x < 40) && (y <= 90): true (x == 50)  (y > 80): false !((x > 20) && (y >= 80)): false Assignment OperatorsAssignment operators are used for assigning a value to the variable. The operand on the left side of the assignment operator is a variable, and the operand on the right side of the assignment operator is a value. The rightside value must be of the same datatype of the leftside variable; otherwise, the compiler will raise an error.
For example: In this example, we are using all logical operators that are listed above. Output: After assignment the value of x is: 40 x+=y: 80 x=y: 40 x*=y: 1600 x/=y: 40 x%=y: 0 Bitwise OperatorsBitwise operators are used for performing the bitwise operations on binary numerals or bit patterns that involve the manipulation of individual bits. Bitwise operators perform the operation on the binary representation of arguments Generally, bitwise operators are less used and relevant for the applications and hyperperformance programs.
For example: In this example, we are using all logical operators that are listed above. Output: Value of 70 in binary 0100 0110 Value of 80 in binary 0101 0000 Value of x & y = 64 Value of x  y = 86 Value of x ^ y = 22 Value of ~ x = 71 Value of x << 2 = 280 Value of x >> 2 = 17 Note: The logic of assignment operators is also applied to Bitwise operators, so they become <<=, >>=, &=, =, ^=.Miscellaneous OperatorsThese are the operators that perform different operations in different circumstances.
Let us try to understand the miscellaneous operators in detail: The Negation Operator ()It is used to change the sign of the value. For example: Output: Value of num1 = 80 Value of num2 = 80 The Concatenation Operator (+)It applies on strings and appends the second string to first. You can understand it by using the following example: Example: Output: HelloWorld Welcome Back The concatenation operator does not add the space between the strings. It concatenates multiple strings in a single statement. If you want to show the space between your strings, then you have to define it manually. In the above example, the string "HelloWorld" does not contain any space, but the second string "Welcome Back" has space because we have manually added it. The Conditional Operator (?)This operator represents the conditional expression. It is also called the 'ternary operator.' Syntax: Where, condition: It refers to the conditional expression. value1: If the condition is true, then this value will be returned. value2: If the condition is false, then this value will be returned. Example: Output: Yes 30 is greater than 20 Type OperatorsIt is a unary operator that returns the data type of the operand. Syntax: You can see the data types and values in the following table that are returned by the typeof operator in JavaScript:
Example: Output: Variable a is number Variable b is boolean Variable c is a string Variable d is a string Variable e is undefined In the above example, the variable e is not defined (or not initialized); that's why the typeof operator is giving its type undefined. If you take the Boolean Values within the quotes, then they will be treated as a string as you can see the variable 'd' in the above example.
Next TopicES6 Loops
