This page is a work in progress.You can help improve it. →

Operators

This section describes TypeScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary, and more.

A complete and detailed list of operators and expressions is also available in the MDN reference.

TypeScript has the following types of operators. This section describes the operators and contains information about operator precedence.

  • Assignment operators
  • Comparison operators
  • Arithmetic operators
  • Bitwise operators
  • Logical operators
  • String operators
  • Conditional (ternary) operator
  • Comma operator
  • Unary operators
  • Relational operators

Assignment operators

An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

There are also compound assignment operators that are shorthand for the operations listed in the following table:

NameShorthand operatorMeaning
Assignmentx = yx = y
Addition assignmentx += yx = x + y
Subtraction assignmentx -= yx = x - y
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Exponentiation assignmentx **= yx = x ** y
Left shift assignmentx <<= yx = x << y
Right shift assignmentx >>= yx = x >> y
Unsigned right shift assignmentx >>>= yx = x >>> y
Bitwise AND assignmentx &= yx = x & y
Bitwise XOR assignmentx ^= yx = x ^ y
Bitwise OR assignment`x= y``x = xy`

Destructuring Assignment

For more complex assignments, the destructuring assignment syntax is a TypeScript expression that makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.

const numbers = ['one', 'two', 'three']
// without destructuring
const one = numbers[0]
const two = numbers[1]
const three = numbers[2]
// with destructuring
const [one, two, three] = numbers

Comparison Operators

A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. Strings are compared based on standard ordering. In most cases, if the two operands are not of the same type, TypeScript attempts to convert them to an appropriate type for the comparison. This behavior generally results in comparing the operands numerically. The sole exceptions to type conversion within comparisons involve the === and !== operators, which perform strict equality and inequality comparisons. These operators do not attempt to convert the operands to compatible types before checking equality. The following table describes the comparison operators in terms of this sample code:

const var1 = 3
const var2 = 4
OperatorDescriptionExamples returning true
Equal (==)Returns true if the operands are equal.3 == var1
"3" == var1
3 == '3'
Not equal (!=)Returns true if the operands are not equal.var1 != 4
var2 != "3"
Strict equal (===)Returns true if the operands are equal and of the same type. See also Object.is and sameness in JS.3 === var1
Strict not equal (!==)Returns true if the operands are of the same type but not equal, or are of different type.var1 !== "3"
3 !== '3'
Greater than (>)Returns true if the left operand is greater than the right operand.var2 > var1
"12" > 2
Greater than or equal (>=)Returns true if the left operand is greater than or equal to the right operand.var2 >= var1
var1 >= 3
Less than (<)Returns true if the left operand is less than the right operand.var1 < var2
"2" < 12
Less than or equal (<=)Returns true if the left operand is less than or equal to the right operand.var1 <= var2
var2 <= 5

Arithmetic operators

An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work as they do in most other programming languages when used with floating point numbers (in particular, note that division by zero produces Infinity). For example:

1 / 2 // 0.5
1 / 2 == 1.0 / 2.0 // this is true

In addition to the standard arithmetic operations (+, -, * /), TypeScript provides the arithmetic operators listed in the following table:

OperatorDescriptionExample
Remainder (%)Binary operator. Returns the integer remainder of dividing the two operands.12 % 5 returns 2.
Increment (++)Unary operator. Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one.If x is 3, then ++x sets x to 4 and returns 4, whereas x++ returns 3 and, only then, sets x to 4.
Decrement (--)Unary operator. Subtracts one from its operand. The return value is analogous to that for the increment operator.If x is 3, then--xsetsxto2and returns2, whereasx--returns3and, only then, setsxto2`.
Unary negation (-)Unary operator. Returns the negation of its operand.If x is 3, then -x returns -3.
Unary plus (+)Unary operator. Attempts to convert the operand to a number, if it is not already.+"3" returns 3.
+true returns 1.
Exponentiation operator (**)Calculates the base to the exponent power, that is, baseexponent2 ** 3 returns 8.
10 ** -1 returns 0.1.

Bitwise operators

Bitwise operators aren't used very often, but they are useful to understand

Logical operators

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.

OperatorUsageDescription
Logical AND
&&
expr1 && expr2
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR
||
expr1 || expr2
Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values,returns true if either operand is true; if both are false, returns false.
Logical NOT
!
!expr
Returns false if its single operand that can be converted to true; otherwise, returns true.

© 2017 - 2022; Built with ♥ in St. Petersburg, Florida.