+ 3

.length() and .length

Whats the difference of .length and .length() in javascript? With an example please

18th Sep 2020, 6:01 PM
Carmen LucĂ­a
Carmen LucĂ­a - avatar
7 Answers
+ 4
Carmen LucĂ­a .length is array data property where as .length() is a string function.(i am not sure, is it js or not) So here data property can be used to set and get value of array length where as length function is used to just for calling or getting length of string. Ex: Array.length = 5 console.log (Array.length) ; console.log("string".length()) ; //doubt not exist in js
18th Sep 2020, 6:21 PM
Jayakrishna 🇼🇳
+ 1
Thanks!
18th Sep 2020, 6:43 PM
Carmen LucĂ­a
Carmen LucĂ­a - avatar
+ 1
You're welcome... But sry i have a confusion,.. Can you add a Refference for length() function you seen.. It will not working in js. Let me check where about you asking..? Carmen LucĂ­a I think there is no length() function exists in javascript..... Both works in Java.
18th Sep 2020, 6:46 PM
Jayakrishna 🇼🇳
+ 1
It was a homework that said something like: "Use .length() for this work. Ex: var fruits = ["Apple", "Orange"] The length of this array is 2" But I think it was a mistake the () and is the normal fruits.length
18th Sep 2020, 7:16 PM
Carmen LucĂ­a
Carmen LucĂ­a - avatar
+ 1
Yes. Both exits in java and works as I said. But in javascript, only field or data property .length exists. And works for arrays and string both.. So there for array : fruits.length returns 2 fruits[0].length returns "Apple" string length; 5
18th Sep 2020, 7:21 PM
Jayakrishna 🇼🇳
+ 1
Thank you
18th Sep 2020, 8:37 PM
Carmen LucĂ­a
Carmen LucĂ­a - avatar
+ 1
You're welcome...
18th Sep 2020, 10:17 PM
Jayakrishna 🇼🇳