what is the effect of the '+' in the following?
var result = jQuery.trim(this.html2val(this.getValueJelement()[0].innerHTML));
result = +result.replace(/[^\d\.-]/g, '');
Unary plus (+) The unary plus operator ( + ) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
The Complete Full-Stack JavaScript Course! The plus(+) sign before the variables defines that the variable you are going to use is a number variable.
$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).
The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.
This is basically a sneaky way of coercing the right-hand operand into a numeric value. E.g.,
> +"42"
42
It converts the operand to a number. In other words, it's basically the same as saying result = parseFloat(result)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With