Hello Readers,
In jquery both works and gives the same result but now the time in current version of jquery we use the jquery.length because jquery.size is depreciated.
Below is the some points about this properties :
jquery.size() and jquery.length() both returns the number of element in an object.
jquery.length() property is faster as compared to jquery.size() method.
jquery.length() property prefered compared to size() because it does not have overhead of a function call.
Very significant difference is that jquery.size() is depreciated so we always use jquery.length() instead of jquery.size().
For Examples:
Counts the number of unordered list li on the page to demonstrate the same according to the jQuery documentation.
$('li').size();
$('li').length;// faster
In the above example, says that the jquery.length() property should be prefered over the jquery.size() function and it does not overhead the function call so it become faster as compared to size()
- jquery.size() method does not accept any arguments.
- Below is the another example (simple unordered list on the page):
Example
<ul>
<li>foo</li>
<li>bar</li>
</ul>
Both .size() and .length identify the number of items :
alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );
This results in two alerts :
Size: 2
Size: 2
0 Comment(s)