Unary Operators in JavaScript
Here are the implementation of Unary Operator in Javascript language:
let a = 10;
let b = 5;
// Unary increment (++)
// Increment 'a' by 1 before using its value
console.log("Unary Increment:", ++a);
// 'a' has been incremented
console.log("a after increment:", a);
// Unary decrement (--)
// Decrement 'b' by 1 before using its value
console.log("Unary Decrement:", --b);
// 'b' has been decremented
console.log("b after decrement:", b);
// Unary plus (+)
let c = -5;
// The unary plus doesn't change the value of 'c'
console.log("Unary Plus:", +c);
// Unary minus (-)
// The unary minus negates the value of 'c'
console.log("Unary Minus:", -c);
// Unary logical NOT (!)
let d = false;
// Logical NOT of false is true
console.log("Unary Logical NOT:", !d);
// Unary bitwise NOT (~)
let e = 1;
// Bitwise NOT of 1 is -2
console.log("Unary Bitwise NOT:", ~e);
Output
Unary Increment: 11 a after increment: 11 Unary Decrement: 4 b after decrement: 4 Unary Plus: -5 Unary Minus: 5 Unary Logical NOT: true Unary Bitwise NOT: -2
Unary Operators in Programming
In programming, operators act as powerful tools for manipulating data and performing various operations. Among these, unary operators stand out, functioning on a single operand to transform or evaluate data in different ways. This post explains the types, implementations, and best practices associated with unary operators across several programming languages.
Table of Content
- What are Unary Operators?
- Types of Unary Operators
- Unary Operators in C
- Unary Operators in C++
- Unary Operators in Java
- Unary Operators in Python
- Unary Operators in C#
- Unary Operators in JavaScript
- Examples and Use Cases of Unary Operator
- Best Practices of Unary Operator