Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the leading `+` in `j = +i + ( i < 0 ? len : 0 )` (taken from jQuery source code) [duplicate]

Tags:

javascript

I found the following snippet in the jQuery source code, in the definition of the eq function:

j = +i + ( i < 0 ? len : 0 ) 

I was surprised by the +i. Rather, I would have expected:

j = i + ( i < 0 ? len : 0 ) 

What's the difference? What the utility of that leading +?

like image 913
Shawn Avatar asked Jun 07 '13 16:06

Shawn


People also ask

Which built in method in jQuery add one or more elements to the end of an array?

push() method adds one or more elements to the end of an array and returns the new length of the array.

What is $$ in jQuery?

$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.

Why JavaScript is not working in HTML?

On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.

What problems do jQuery and Ajax solve?

jQuery's syntax is designed to make it easier to navigate a document, select DOM elements, create animations, handle events, and develop Ajax applications. jQuery also provides capabilities for developers to create plug-ins on top of the JavaScript library.


1 Answers

+i coerces to number. As an example, try "1" + 1 versus +"1" + 1 (the former is "11" while the latter is 2)

like image 58
SheetJS Avatar answered Oct 01 '22 22:10

SheetJS