JavaScript Unary Operators (original) (raw)

Last Updated : 25 Nov, 2024

JavaScript **Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc.

Unary Plus (+) Operator

The unary plus (+) converts an operand into a number, if possible. It is commonly used to ensure numerical operations on variables that may contain numeric strings. If the operand is a string that represents a valid number, it will be converted to a number. Otherwise, it will evaluate to NaN (Not-a-Number).

JavaScript `

let s1 = "12";

// Using unary plus to convert string to number let x = +s1; console.log(x);

// Here we are using typeof operator console.log(typeof (x))

// "Geeks" cannot be converted to a number let s2 = +"Geeks"; console.log(s2);

`

Unary Minus (-) Operator

The Unary Negation (-) operator is used to convert its operand to a negative number if it isn’t already a negative number.

JavaScript `

let s1 = "12";

// Unary negation, negates the // value of number let x = -s1;

console.log(x);

// Unary negation, tries to convert // 'Geeks' to a number let s2 = -"Geeks";

console.log(s2);

`

Unary Increment (++) Operator

The unary increment operator (++) adds 1 to its operand's value and evaluates to the updated value. It can be used as a postfix or prefix operator.

// Case 1: Postfix let x = 12; let y = x++; console.log(x); console.log(y);

// Case 2: Preifix x = 10; y = ++x; console.log(x); console.log(y);

`

Unary Decrement (--) Operator

The unary decrement operator (--) subtracts 1 from its operand's value and evaluates it to the updated value, and we can use it as a postfix or prefix operator.

let x = 8; let y = x--; console.log(x); console.log(y);

x = 15; y = --x; console.log(x); console.log(y);

`

Logical NOT (!) Operator

The logical NOT (!) is a unary operator that negates the Boolean value of an operand, converting true to false and false to true.

JavaScript `

let x = false; let y = !x; console.log(y);

x = true; y = !x; console.log(y);

`

Bitwise NOT (~) Operator

The bitwise NOT (~) is a unary operator that inverts all the bits of its operand, converting each 0 to 1 and each 1 to 0.

JavaScript `

let x = 10;

// Bitwise NOT, inverts all bits of 'num' let y = ~x;

console.log(y);

`

typeof Operator

The JavaScript typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.

JavaScript `

let x = 18; let s = "GeeksforGeeks"; let isTrue = true; let obj = { name: "Aman", age: 21 }; let undefinedVar;

console.log(typeof x); console.log(typeof s); console.log(typeof isTrue); console.log(typeof obj); console.log(typeof undefinedVar);

`

Output

number string boolean object undefined

delete Operator

The delete operator in JavaScript removes a property from an object. If no other references exist, the property's memory is automatically released.

JavaScript `

let person = { name: "Ankit", age: 21, city: "Noida" };

console.log(person);

delete person.age;

console.log(person);

`

Output

{ name: 'Ankit', age: 21, city: 'Noida' } { name: 'Ankit', city: 'Noida' }

void Operator

The void operator evaluates the given expression and then returns undefined.

JavaScript `

function myFunction() { return void 0; } console.log(myFunction());

`