[image] Aisha1: lastNameLength = lastName; This is just setting the variable lastNameLength to the value of lastName, which is the string “Lovelace”. You want to set lastNameLength to the length of the string in lastName. Answer from bbsmooth on forum.freecodecamp.org
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › length
String: length - JavaScript | MDN
3 weeks ago - Answer:"; console.log(`${str} ${str.length}`); // Expected output: "Life, the universe and everything. Answer: 42" A non-negative integer. This property returns the number of code units in the string. JavaScript uses UTF-16 encoding, where each Unicode character may be encoded as one or two code units, so it's possible for the value returned by length to not match the actual number of Unicode characters in the string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript | MDN
The length data property of an Array instance represents the number of slots in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. It may be greater than the number of elements if the array is sparse.
Discussions

Basic JavaScript - Find the Length of a String
Tell us what’s happening: Describe your issue in detail here. Your code so far Use the .length property to set lastNameLength to the number of characters in lastName . I was told to do the above instruction but i did and i can’t see to know what am getting wrong at. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
June 21, 2023
forms - How to check string length with JavaScript - Stack Overflow
I want to get the string length when a key is pressed like StackOverflow does. I have tried to do this with onblur, but it's not working. How do I do this? More on stackoverflow.com
🌐 stackoverflow.com
Difference between .length() and .length?
The .length of a String is a property, not a function, as per ECMAScript spec . More on reddit.com
🌐 r/learnjavascript
3
15
March 5, 2016
Length of a JavaScript object - Stack Overflow
I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Scaler
scaler.com › home › topics › string length in javascript
String Length in JavaScript - Scaler Topics
April 9, 2024 - The string length property in JavaScript returns the length of string in UTF-16 code units. It returns 0 when the string is empty, it's length. JavaScript uses a UTF-16 string format to depict the most common characters, consisting of a 16-bit data unit. The value returned by length does not ...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Basic JavaScript - Find the Length of a String - Curriculum Help - The freeCodeCamp Forum
June 21, 2023 - Tell us what’s happening: Describe your issue in detail here. Your code so far Use the .length property to set lastNameLength to the number of characters in lastName . I was told to do the above instruction but i did…
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Function › length
Function: length - JavaScript | MDN
July 10, 2025 - The length data property of a Function instance indicates the number of parameters expected by the function.
Find elsewhere
Top answer
1 of 11
47

As for the question which event you should use for this: use the input event, and fall back to keyup/keydown in older browsers.

Here’s an example, DOM0-style:

someElement.oninput = function() {
  this.onkeydown = null;
  // Your code goes here
};
someElement.onkeydown = function() {
  // Your code goes here
};

The other question is how to count the number of characters in the string. Depending on your definition of “character”, all answers posted so far are incorrect. The string.length answer is only reliable when you’re certain that only BMP Unicode symbols will be entered. For example, 'a'.length == 1, as you’d expect.

However, for supplementary (non-BMP) symbols, things are a bit different. For example, '𝌆'.length == 2, even though there’s only one Unicode symbol there. This is because JavaScript exposes UCS-2 code units as “characters”.

Luckily, it’s still possible to count the number of Unicode symbols in a JavaScript string through some hackery. You could use Punycode.js’s utility functions to convert between UCS-2 strings and Unicode code points for this:

// `String.length` replacement that only counts full Unicode characters
punycode.ucs2.decode('a').length; // 1
punycode.ucs2.decode('𝌆').length; // 1 (note that `'𝌆'.length == 2`!)

P.S. I just noticed the counter script that Stack Overflow uses gets this wrong. Try entering 𝌆, and you’ll see that it (incorrectly) counts as two characters.

2 of 11
15

UPDATE: Since I wrote this, the input event has gotten a decent level of support. It is still not 100% in IE9, so you will have to wait a bit until IE9 is fully phased out. In light of my answer to this question, however, input is more than a decent replacement for the method I've presented, so I recommend switching.

Use keyup event

var inp = document.getElementById('myinput');
var chars = document.getElementById('chars');
inp.onkeyup = function() {
  chars.innerHTML = inp.value.length;
}
<input id="myinput"><span id="chars">0</span>

EDIT:

Just a note for those that suggest keydown. That won't work. The keydown fires before character is added to the input box or textarea, so the length of the value would be wrong (one step behind). Therefore, the only solution that works is keyup, which fires after the character is added.

🌐
Mimo
mimo.org › glossary › javascript › array-length
JavaScript Array Length: Master Data Handling
Quick Answer: How to Get the Length of an Array in JS To get the number of elements in a JavaScript array, you use the .length property. It is a property, not a method, so you do not use parentheses ().
🌐
YouTube
youtube.com › junior developer central
Javascript String Length: How to determine the size of a string - YouTube
In this tutorial, we’ll take a look at the JavaScript String Length property and how you can use this to determine the size of a string. Don’t forget to subs...
Published: January 31, 2019
Views: 3K
🌐
Tabnine
tabnine.com › home › how to use the string length property in javascript
How to Use the String length Property in JavaScript - Tabnine
July 25, 2024 - The length property of a String object returns the number of characters contained in a string. const str = 'Get the length of this string'; console.log(str.length); // Expected output: 29 In the example above, str is a String containing 29 ...
🌐
Codecademy
codecademy.com › docs › javascript › storage › .length
JavaScript | Storage | .length | Codecademy
July 8, 2025 - In JavaScript, the .length property is used to determine the number of elements, characters, or items in a given data structure, such as arrays or strings.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-length
JavaScript string.length - GeeksforGeeks
June 1, 2026 - The string.length is a property in JS which is used to find the length of a given string.
🌐
Stack Overflow
stackoverflow.com › questions › 5223 › length-of-a-javascript-object
Length of a JavaScript object - Stack Overflow
I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? const myObject = new Object(); myObject["firstname"] = "Gareth";
Top answer
1 of 5
5

JavaScript exhibits zero based indexing, that is, the first element in an array or in a string is at position 0. Therefore, an array or string of length n has elements going from position 0 to n - 1, with element at position n being undefined. This means that an array or string with n elements has the last element at n - 1, which is accessed as someArrayString[n - 1].

.length returns the length of an array or a string. Hence, the last element of an array or a string is found at someArrayString.length - 1 which is accessed as someArrayString[someArrayString.length - 1].

From the code, it can be inferred that word is a string. Therefore, the line word[word.length-1] accesses the last char (letter) in the word (although it actually accesses the last code unit but in ASCII a code unit correspond with a 1 byte ASCII char).

For example, the string var word = "JavaScript" has length 10. With J at position 0 and t at position 9. In other words, word[0] == 'J' and word[word.length - 1] == 't'

2 of 5
1

Let's say your word = 'People';

word.length would return 6 which is the character number in your word.

Since arrays (in this case string because .length can be used in strings too) start from index 0, word.length-1 would give you the 5th element of your string, which is the last character of your word.

In your code, if (word[word.length-1] === '.' || word[word.length-1] === '!') checks if the last character of a word is a dot (.) or exclamation point (!) so you can count how many sentences there are in a given string.

Top answer
1 of 3
7
A function is a group of statements to perform an action when called on. A property on the other hand holds information. Think of a property as a variable (it is a variable) that variable stores information so in this case the length variable stores the length of an array and the function (method) push and pop does an action they either remove elements from an array or add them. I'm believe you want to know how the length property could know the length of a string with out calling a function? I don't know the answer to that if it's so but think of it like this you create a string var string = "Hello World!"; when this string is created maybe i dont know but maybe a function is called right away like getLength and it stores it in the length variable. thus using string.length gives the length. Hope I haven't confused you but i'll clear it up if I need to
2 of 3
8
In relation to JavaScript objects, words that end with a set of parentheses are methods (which are just functions that are associated with an object). Words that do not end in a set of parentheses are properties (variables that describe the state of the object). An easy way to think about this is: methods perform actions while properties only describe things. Length is not a method, it is a property. It doesn't actually do anything but return the length of an array, a string, or the number of parameters expected by a function. When you use .length, you are just asking the JavaScript interpreter to return a variable stored within an object; you are not calling a method. For example: ```javascript var x = "hello world" x.length -> 11 // This is a property. It just describes the state of the variable. x.toUpperCase() -> "HELLO WORLD" // This is a method. It actually performed an action // and returned an independent piece of data. ``` EDIT - I see someone answered this question with a similar example before I could submit my post. I'll leave this answer up in the hopes it provides you with some clarity.
🌐
SitePoint
sitepoint.com › blog › javascript › do i use .size() or .length in javascript?
Do I use .size() or .length in Javascript? — SitePoint
February 12, 2024 - So in a nutshell, I use .length until someone gives me a substantial reason not to. In JavaScript, both size and length are used to determine the number of elements in an object. However, they are used with different types of objects. The length property is used with array objects, while the ...
🌐
Nature
nature.com › nature communications › for authors › article
Article | Nature Communications
In the meantime, to ensure continued support, we are displaying the site without styles and JavaScript. ... Nature Communications publishes original research in one format, Articles, which may range in length from short communications through to more in-depth studies.