support@90-10.dev

JavaScript Unary Operators

In JavaScript, operators are used to manipulate or perform actions on operands (values or variables). Unary operators, specifically, are those that operate on a single operand and can either precede or follow the operand, depending on the operation being performed.

Unary Plus (+)

The unary plus operator is used to convert its operand into a number. If the operand is already a number, it has no effect. However, if the operand is a non-numeric value, it attempts to convert it into a number.

let x = "42";
let y = +a;
console.log(typeof y);  // prints: number
console.log(y);         // prints: 42

Unary Negation (-)

The unary negation operator negates the value of its numeric operand. Similar to the unary plus, it converts non-numeric values into numbers before negating them.

let x = "42";
let y = -x;
console.log(typeof y);  // prints: number
console.log(y);         // prints: -42

Logical NOT (!)

The logical NOT operator is used to negate the boolean value of its operand. It returns true if the operand is falsy (e.g., null, undefined, false, 0, NaN, or an empty string) and false otherwise.

let x = "42";
console.log(!x);  // prints: false
let y;
console.log(!y);  // prints: true

Bitwise NOT (~)

The Bitwise NOT operator that inverts the bits of its operand. This will also flip the sign bit (the leftmost) so the result is negative.
NB: Bitwise operators treat their operands as a sequence of 32 bits.

10          // binary: 00000000 00000000 00000000 00001010
~10 = -11   // binary: 11111111 11111111 11111111 11110101

Increment (++) and Decrement (--)

These unary operators are used to increase or decrease the value of a numeric operand by 1. They come in two forms: prefix (e.g., ++a) and postfix (e.g., a++). The difference between the two forms lies in the order of evaluation when used in an expression.

let x = 42;
let y = x++;
console.log(x);  // prints: 43
console.log(y);  // prints: 42
y = ++x;
console.log(y);  // prints: 44
console.log(y);  // prints: 44

typeof

The typeof operator returns a string representing the data type of its operand. It is helpful in determining the type of a variable or value.

let x = 42;
let y = "Hello, World!";
console.log(typeof x);  // prints: number
console.log(typeof y);  // prints: string

void

The void operator evaluates an expression and returns undefined. It is often used in cases where a function should not return any value, such as with hyperlink onclick event handlers.

function doSomething() {
  console.log("Action performed");
  return "result";
}

let result = void doSomething();
console.log(result); // undefined

delete

The delete operator is used to remove a property from an object. It returns true if the deletion is successful or if the property doesn't exist, and false if the property is non-configurable.

const person = {
    name: 'Charlie',
    age: 30
};
console.log(delete person.age);  // prints:  true


Take Away

JavaScript unary operators are powerful tools that can help you write more efficient and clean code. Mastering these operators will enhance your problem-solving skills and provide you with a deeper understanding of JavaScript's inner workings. From basic arithmetic and logical operations to manipulating object properties and handling edge cases, unary operators play a critical role in JavaScript programming.