Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of prepended plus sign in javascript

what is the effect of the '+' in the following?

var result = jQuery.trim(this.html2val(this.getValueJelement()[0].innerHTML));
    result = +result.replace(/[^\d\.-]/g, '');
like image 774
argyle Avatar asked May 17 '12 17:05

argyle


People also ask

What does plus sign do in JavaScript?

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.

What is plus sign in front of variable JavaScript?

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.

What is '$' in JavaScript?

$ 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).

What does it mean to write two plus signs after a number type variable JavaScript?

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.


2 Answers

This is basically a sneaky way of coercing the right-hand operand into a numeric value. E.g.,

> +"42"
42
like image 97
jmar777 Avatar answered Sep 26 '22 02:09

jmar777


It converts the operand to a number. In other words, it's basically the same as saying result = parseFloat(result).

like image 44
Elliot Bonneville Avatar answered Sep 25 '22 02:09

Elliot Bonneville