JavaScript includes operators same as other languages. An operator performs some operation on single or multiple operands [data value] and produces a result. For example, in 1 + 2
, the +
sign is an operator and 1 is left side operand and 2 is right side operand. The +
operator performs the addition of two numeric values and returns a result.
operator operator
JavaScript includes following categories of operators.
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Conditional Operators
- Ternary Operator
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations between numeric operands.
+ | Adds two numeric operands. |
- | Subtract right operand from left operand |
* | Multiply two numeric operands. |
/ | Divide left operand by right operand. |
% | Modulus operator. Returns remainder of two operands. |
++ | Increment operator. Increase operand value by one. |
-- | Decrement operator. Decrease value by one. |
The following example demonstrates how arithmetic operators perform different tasks on operands.
var x = 5, y = 10;
var z = x + y; //performs addition and returns 15
z = y - x; //performs subtraction and returns 5
z = x * y; //performs multiplication and returns 50
z = y / x; //performs division and returns 2
z = x % 2; //returns division remainder 1
The ++
and --
operators are unary operators. It works with either left or right operand only. When used with the left operand, e.g., x++
, it will increase the value of x
when the program control goes to the next statement. In the same way, when it is used with the right operand, e.g., ++x
, it will increase the
value of x
there only. Therefore, x++
is called post-increment, and ++x
is called pre-increment.
var x = 5;
x++; //post-increment, x will be 5 here and 6 in the next line
++x; //pre-increment, x will be 7 here
x--; //post-decrement, x will be 7 here and 6 in the next line
--x; //pre-decrement, x will be 5 here
String Concatenation
The +
operator performs concatenation operation when one of the operands is of string type. The following example demonstrates string concatenation even if one of the operands is a string.
var a = 5, b = "Hello ", c = "World!", d = 10;
a + b; //returns "5Hello "
b + c; //returns "Hello World!"
a + d; //returns 15
b + true; //returns "Hello true"
c - b; //returns NaN; - operator can only used with numbers
Comparison Operators
JavaScript provides comparison operators that compare
two operands and return a boolean value true
or false
.
== | Compares the equality of two operands without considering type. | |||||||||||
=== | Compares equality of two operands with type. | |||||||||||
!= | Compares inequality of two operands. | |||||||||||
> | Returns a boolean value true if the left-side value is greater than the right-side value; otherwise, returns false. | |||||||||||
< | Returns a boolean value true if the left-side value is less than the right-side value; otherwise, returns false. | |||||||||||
>= | Returns a boolean value true if the left-side value is greater than or equal to the right-side value; otherwise, returns false. | |||||||||||
b; // returns false
a < b; // returns true
a >= b; // returns false
a b] || [a == b]; // returns false
[a < b] || [a == b]; // returns true
![a < b]; // returns false
![a > b]; // returns true
Assignment OperatorsJavaScript provides the assignment operators to assign values to variables with less key strokes.
Ternary Operator JavaScript provides a special operator called ternary operator ? : ; The ternary operator starts with conditional expression followed by the
Want to check how much you know JavaScript? What are the arithmetic operators in JavaScript?Arithmetic operators. Which of the following is an arithmetic binary operator in JavaScript?Arithmetic operators. Which of the following are JavaScript operators?JavaScript includes various categories of operators: Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Conditional operators. Ternary operator ?: is a short form of if-else condition.
What does && and || mean in JavaScript?May 25, 2016. Similar to other C-like programming languages, JavaScript defines the two operators && and || which represent the logical AND and OR operations, respectively.
Chủ Đề |