JavaScript Operators
- Previous Page JS Const
- Next Page JS Arithmetic
JavaScript Operators
Example
Assign values to variables and add them together:
var x = 7; // Assign the value 7 to x var y = 8; // Assign the value 8 to y var z = x + y; // Assign the value 15 to z (x + y)
AssignmentOperator (=
)Assign a value to the variable.
Assignment
var x = 15;
AdditionOperator (+
)Add numbers:
Addition
var x = 7; var y = 8; var z = x + y;
MultiplicationOperator (*
)Multiply numbers:
Multiplication
var x = 7; var y = 8; var z = x * y;
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on numbers:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
++ | Increment |
-- | Decrement |
Note:JS ArithmeticThis chapter fully describes arithmetic operators.
JavaScript Assignment Operator
The assignment operator assigns a value to the JavaScript variable.
Operator | Example | Is equivalent to |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
Addition assignment operator (+=
)Add a value to the variable.
Assignment
var x = 7; x += 8;
Note:JS AssignmentThis chapter fully describes the assignment operator.
JavaScript String Operators
+
The operator can also be used to concatenate (concatenate, cascading) strings.
Example
txt1 = "Bill"; txt2 = "Gates"; txt3 = txt1 + " " + txt2;
The result of txt3 will be:
Bill Gates
+=
The assignment operator can also be used to add (cascading) strings:
Example
txt1 = "Hello "; txt1 += "Kitty!";
The result of txt1 will be:
Hello Kitty!
Tip:When used for strings:+
The operator is called the cascading operator.
Addition of strings and numbers
Adding two numbers will return the sum, but adding a number and a string will return a string:
Example
x = 7 + 8; y = "7" + 8; z = "Hello" + 7;
The result of x, y, and z will be:
15 78 Hello7
Tip:If you add a number and a string, the result will be a string!
JavaScript Comparison Operators
Operator | Description |
---|---|
== | Equal |
=== | Equal value and type |
!= | Not equal |
!== | Unequal value or type |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
? | Trinary operator |
Note:JS ComparisonThis chapter fully describes the comparison operators.
JavaScript Logical Operators
Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Note:JS ComparisonThis chapter fully describes the logical operators.
JavaScript Type Operators
Operator | Description |
---|---|
typeof | Returns the type of the variable. |
instanceof | Returns true if the object is an instance of the object type. |
Note:JS Type ConversionThis chapter fully describes the type operators.
JavaScript Bitwise Operators
Bitwise operators handle 32-bit numbers.
Any numeric operand in this operation will be converted to a 32-bit number. The result will be converted back to a JavaScript number.
Operator | Description | Example | Is equivalent to | Result | Decimal |
---|---|---|---|---|---|
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | ~5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Zero-filled left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Signed right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>>> | Zero-filled right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |
The previous example uses a 4-bit unsigned example. However, JavaScript uses 32-bit signed numbers.
Therefore, in JavaScript, ~5 will not return 10, but -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010.
Note:We will JS BitwiseThis chapter explains bitwise operators in detail.
textbook
For more information on JavaScript OperatorsFor more information on the knowledge, please read the relevant content in the advanced JavaScript tutorial:
- ECMAScript Unary Operators
- Unary operators have only one parameter, which is the object or value to be operated on. This section explains the simplest operator in ECMAScript - unary operator.
- ECMAScript Bitwise Operators
- Bitwise operators operate at the low level of numbers. This section delves into the knowledge of integers and introduces various bitwise operators of ECMAScript.
- ECMAScript Boolean Operators
- Boolean operators are very important. This section delves into the three Boolean operators: NOT, AND, and OR.
- ECMAScript Multiplicative Operators
- This section explains the multiplicative operators of ECMAScript: multiplication, division, and modulus operators, as well as their special behaviors.
- ECMAScript Additive Operators
- This section explains the additive operators of ECMAScript: addition, subtraction operators, and their special behaviors.
- ECMAScript Relational Operators
- Relational operators perform comparison operations. This section explains the conventional comparison methods of relational operators, as well as how to compare strings with numbers.
- ECMAScript Equality Operators
- Equality operators are used to determine if variables are equal. ECMAScript provides two sets of equality operators: equal to and not equal to, as well as strictly equal to and strictly not equal to.
- ECMAScript Conditional Operators
- This section explains the conditional operator in ECMAScript.
- ECMAScript Assignment Operators
- This section explains the assignment operator in ECMAScript.
- ECMAScript Comma Operator
- This section explains the comma operator in ECMAScript.
- Previous Page JS Const
- Next Page JS Arithmetic