- String Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Ternary Operators
- Bitwise Operators
- Unary Operators
In the previous tutorial, we have learned how to connect between two or multiple strings together. We had used the mathematical
+ is the only sign that is considered as string operator. Otherwise, the
+ sign is considered as arithmetic addition operator. Let’s see an example to clear it.
String Operator Example:
var myString1 = "Welcome "; var myString2 = "to "; var myString3 = "PrograCoding"; console.log( myString1 + myString2 + myString2); // or you can directly use in a context console.log( "Welcome " + "to " + "PrograCoding");
|Addition (||Simply add two or more numerical numbers (eg: |
|Subtraction (||Subtract two numbers from one to another (e.g: |
|Multiplication (||Multiplies two or more numerical values (e.g: |
|Division (||Simple divide two numbers (e.g: |
|Reminder (||It actually consists of division reminder (eg:|
Some arithmetic operators or combination of arithmetic operators may be referred to as other operators. For instance, now,
x++ is an increment operator here,
++ is an increment operator. We will talk about the derivatives operators later that was produced from arithmetic operator’s sign.
Arithmetic operators example:
console.log (4 + 9); // addition console.log(12 - 4); //subtruction console.log(10 * 8); //multiplication console.log(80 / 10) //division console.log(35 /16) //reminder
Output in the JS console
We will learn more about arithmetic operators in details in a later chapter.
= ). While declaring a variable the assignment operator is used to assign the value.
It does not mean x is equal to 5.
Assignment Operators Example:
var x = 10; // 10 has been assigned in the variable x document.write ( x ); // expected output 10. Because x has hold 10 as its value as defined
The result of the comparison operator always returns boolean values,
TRUE using a comparison operator. Then a certain block of code will be executed otherwise will execute another block.
==sign compares two operands that if they are true in value.
===sign compare two operands that if they are true in value and type.
!=sign compare two operands that if they are false.
Comparision Operator Example:
var x = 6; console.log( x == 3); console.log( x != 3 ); console.log( x = 6);
We will learn in details about all the comparison operators and their method in a later chapter.
In general, logical operators are used with boolean values but it also can be used with non-boolean values. When it is used with boolean values then it returns boolean values and when it is used with non-boolean values it returns non-boolean values.
|Logical AND (||If both two operands are |
|Logical OR (||If one of the operands is |
|Logical NOT (||If a single operand can be converted to |
condition ? value1(true) : value2(false);
Ternary Operator Example:
var status = ( age >= 18) ? "adult" ; "minor";
We will learn more about the ternary operator in details in a later chapter.
Bitwise operator performs binary logical operation internally and represents the operands in specified format rather than binary format. It can decimal, octal or hexadecimal format. But in general, the default format is decimal.
While operation, all the operands is initially converted to binary (zeroes and ones). Then after the operation with the binary format, the final result is represented as default decimal format.
Bitwise Operator example:
console.log( 5 & 13 ); // here & is the Bitwise logical AND operator //expected output 5
We will learn everything about Bitwise operator in a later chapter.
Till now, we have learned many operators. Basically, we have worked with two or three operands with one operator for performing different kinds of calculations. But unlike those operators, unary operators are different from them. Unary operators use operand operator or operator operand syntax. The following types consist of unary operators.
- x–, –x: It is a decrement operator. It also used in the loop. But unlike increment operators, it counts from a higher value to lower value.
- delete: It is actually an object used to delete an element. It can be used to delete either an element from an array or object.
- void: this operator evaluates a given expression and then the expression terminates working after the first evaluation.
We learn more about unary operators and their types in details in the later chapters.