pub enum UnaryOp {
IncrementPost,
IncrementPre,
DecrementPost,
DecrementPre,
Minus,
Plus,
Not,
Tilde,
TypeOf,
Delete,
Void,
}
Expand description
A unary operator is one that takes a single operand/argument and performs an operation.
A unary operation is an operation with only one operand. This operand comes either before or after the operator. Unary operators are more efficient than standard JavaScript function calls.
More information:
Variants
IncrementPost
The increment operator increments (adds one to) its operand and returns a value.
Syntax: ++x
This operator increments and returns the value after incrementing.
More information:
IncrementPre
The increment operator increments (adds one to) its operand and returns a value.
Syntax: x++
This operator increments and returns the value before incrementing.
More information:
DecrementPost
The decrement operator decrements (subtracts one from) its operand and returns a value.
Syntax: --x
This operator decrements and returns the value before decrementing.
More information:
DecrementPre
The decrement operator decrements (subtracts one from) its operand and returns a value.
Syntax: x--
This operator decrements the operand and returns the value after decrementing.
More information:
Minus
The unary negation operator precedes its operand and negates it.
Syntax: -x
Converts non-numbers data types to numbers like unary plus, however, it performs an additional operation, negation.
More information:
Plus
The unary plus operator attempts to convert the operand into a number, if it isn’t already.
Syntax: +x
Although unary negation (-
) also can convert non-numbers, unary plus is the fastest and preferred
way of converting something into a number, because it does not perform any other operations on the number.
It can convert string
representations of integers and floats, as well as the non-string values true
, false
, and null
.
More information:
Not
Returns false
if its single operand can be converted to true
; otherwise, returns true
.
Syntax: !x
Boolean values simply get inverted: !true === false
and !false === true
.
Non-boolean values get converted to boolean values first, then are negated.
This means that it is possible to use a couple of NOT operators in series to explicitly
force the conversion of any value to the corresponding boolean primitive.
More information:
Tilde
Performs the NOT operator on each bit.
Syntax: ~x
NOT a
yields the inverted value (or one’s complement) of a
.
Bitwise NOTing any number x yields -(x + 1). For example, ~-5 yields 4.
More information:
TypeOf
The typeof
operator returns a string indicating the type of the unevaluated operand.
Syntax: typeof x
or typeof(x)
The typeof
is a JavaScript keyword that will return the type of a variable when you call it.
You can use this to validate function parameters or check if variables are defined.
There are other uses as well.
More information:
Delete
The JavaScript delete
operator removes a property from an object.
Syntax: delete x
Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory. Memory management is done indirectly via breaking references. If no more references to the same property are held, it is eventually released automatically.
The delete
operator returns true
for all cases except when the property is an
own
non-configurable
property, in which case, false
is returned in non-strict mode.
More information:
Void
The void
operator evaluates the given expression
and then returns undefined
.
Syntax: void x
This operator allows evaluating expressions that produce a value into places where an
expression that evaluates to undefined
is desired.
The void
operator is often used merely to obtain the undefined
primitive value, usually using void(0)
(which is equivalent to void 0
). In these cases, the global variable undefined can be used.
When using an immediately-invoked function expression,
void
can be used to force the function keyword to be treated as an expression instead of a declaration.
More information: