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:

  1. Negating values
  2. Incrementing and decrementing values
  3. Performing type conversion
  4. Accessing properties and invoking methods

List of Unary Operators

Here are some important unary operators available in TypeScript:

  1. Unary plus (+): Converts the operand to a number, if not already a number.
  2. Unary minus (-): Negates the operand.
  3. Pre-increment (++): Increments the operand by 1 before the evaluation.
  4. Post-increment (++): Increments the operand by 1 after the evaluation.
  5. Pre-decrement (--): Decrements the operand by 1 before the evaluation.
  6. Post-decrement (--): Decrements the operand by 1 after the evaluation.
  7. Bitwise not (~): Inverts the bits of the operand.
  8. Logical not (!): Negates the logical value of the operand.
  9. typeof: Returns the type of the operand.
  10. void: Evaluates the operand without returning a value.
  11. 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.

let strNumber: string = "42"; let numericValue: number = +strNumber; console.log(numericValue); // Result: 42

Here, the unary plus converts the string "42" to the numeric value 42.

Unary Minus (-)

The unary minus operator (-) negates its operand.

let positiveNumber: number = 7; let negativeNumber: number = -positiveNumber; console.log(negativeNumber); // Result: -7

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.

let x: number = 5; let preIncremented: number = ++x; let postIncremented: number = x++; console.log(preIncremented, postIncremented); // Result: 6, 6

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.

let y: number = 8; let preDecremented: number = --y; let postDecremented: number = y--; console.log(preDecremented, postDecremented); // Result: 7, 7

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.

let num: number = 5; let invertedBits: number = ~num; console.log(invertedBits); // Result: -6

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.

let isTrue: boolean = true; let isNotTrue: boolean = !isTrue; console.log(isNotTrue); // Result: false

The logical NOT negates the Boolean value, turning true into false.

typeof

The typeof operator returns a string representing the type of its operand.

let variable: any = 42; let typeOfVariable: string = typeof variable; console.log(typeOfVariable); // Result: "number"

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.

function showAlert(): void { alert("Hello, TypeScript!"); }

The void indicates that the showAlert function does not return any value.

delete

The delete operator deletes a property from an object.

let person = { name: "Bobby", age: 30 }; delete person.age; console.log(person); // Result: { name: "Bobby" }

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.