Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this line of javascript do?

Tags:

javascript

While looking at the jslint code conventions I saw this line:

total = subtotal + (+myInput.value);

What is the purpose of the second '+'?

like image 700
chills42 Avatar asked Mar 04 '09 16:03

chills42


People also ask

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

What does '$' do in JavaScript?

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

Does JavaScript go line by line?

JavaScript is actually interpreted line by line.

What are the 5 JavaScript statements?

JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments.


1 Answers

The unary plus is there for completeness, compared with the familiar unary minus (-x). However it has the side effect, relied upon here, of casting myInput.value into a Number, if it's something else such as a String:

alert(1+'2'); // 12
alert(1+(+'2')); // 3
like image 64
bobince Avatar answered Oct 07 '22 02:10

bobince