TypeScript Unary operators
Unary operators in TypeScript operate on a single operand, modifying its value or performing a specific operation on it. They are essential for various tasks, including:
- Negating values
- Incrementing and decrementing values
- Performing type conversion
- Accessing properties and invoking methods
List of Unary Operators
Here are some important unary operators available in TypeScript:
- Unary plus (+): Converts the operand to a number, if not already a number.
- Unary minus (-): Negates the operand.
- Pre-increment (++): Increments the operand by 1 before the evaluation.
- Post-increment (++): Increments the operand by 1 after the evaluation.
- Pre-decrement (--): Decrements the operand by 1 before the evaluation.
- Post-decrement (--): Decrements the operand by 1 after the evaluation.
- Bitwise not (~): Inverts the bits of the operand.
- Logical not (!): Negates the logical value of the operand.
- typeof: Returns the type of the operand.
- void: Evaluates the operand without returning a value.
- delete: Deletes a property from an object.
TypeScript unary operators with detailed explanations and examples:
Unary Plus (+)
The unary plus operator (+) converts its operand to a number, if it is not already a number.
Here, the unary plus converts the string "42" to the numeric value 42.
Unary Minus (-)
The unary minus operator (-) negates its operand.
The unary minus negates the positive number, resulting in a negative value.
Pre-increment (++) and Post-increment (++)
The pre-increment operator (++) increments the operand by 1 before the evaluation, while the post-increment operator increments the operand after the evaluation.
In this example, preIncremented is 6 because x is incremented before the assignment, while postIncremented is 6 because x is incremented after the assignment.
Pre-decrement (--) and Post-decrement (--)
Similar to increment, pre-decrement (--) decrements the operand by 1 before the evaluation, and post-decrement decrements the operand after the evaluation.
Here, preDecremented is 7, and postDecremented is 7 after the respective decrement operations.
Bitwise NOT (~)
The bitwise NOT operator (~) inverts the bits of its operand.
The bitwise NOT inverts the bits of the binary representation of the number.
Logical NOT (!)
The logical NOT operator (!) negates the logical value of its operand.
The logical NOT negates the Boolean value, turning true into false.
typeof
The typeof operator returns a string representing the type of its operand.
In this example, typeOfVariable is the string "number" representing the type of the variable.
void
The void operator evaluates the operand without returning a value. It is often used with functions to indicate they do not return a value.
The void indicates that the showAlert function does not return any value.
delete
The delete operator deletes a property from an object.
Here, the delete operator removes the age property from the person object.
Type Conversion
The unary plus operator can convert non-numeric values to numbers, allowing you to perform mathematical operations on them.
Pre-increment and Post-increment
These operators differ in the order of evaluation. Pre-increment modifies the operand before using its value, while post-increment modifies the operand after using its value.
Conclusion
TypeScript unary operators are single-operand operators that perform various operations, such as converting to a number, negating, incrementing, decrementing, bitwise and logical operations, type checking, and property deletion. These operators provide powerful tools for manipulating values, controlling flow, and performing type-related operations in TypeScript code.