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.

Answer from Mathias Bynens on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › length
String: length - JavaScript | MDN
July 10, 2025 - The language specification requires strings to have a maximum length of 253 - 1 elements, which is the upper limit for precise integers.
Discussions

Basic JavaScript - Find the Length of a String
You can find the length of a String value by writing .length after the string variable or string literal. console.log("Alan Peter".length); The value 10 would be displayed in the console. Note that the space character between “Alan” and “Peter” is also counted. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
November 9, 2023
How does String.length work in JavaScript? - Stack Overflow
A string is immutable in JavaScript. a += "somestring" doesn't change the length of a string but makes a new string. More on stackoverflow.com
🌐 stackoverflow.com
How to get length of string in javascript without using native length method - Stack Overflow
I am working on a practice problem: Return the length of a string without using javascript's native string.length method. The only ways I could think of would be substring or slice, but I'm stumped. More on stackoverflow.com
🌐 stackoverflow.com
javascript - Create a string of variable length, filled with a repeated character - Stack Overflow
So, my question has been asked by someone else in it's Java form here: Java - Create a new String instance with specified length and filled with specific character. Best solution? . . . but I'm lo... More on stackoverflow.com
🌐 stackoverflow.com
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.

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Find the Length of a String - JavaScript - The freeCodeCamp Forum
November 9, 2023 - You can find the length of a String value by writing .length after the string variable or string literal. console.log("Alan Peter".length); The value 10 would be displayed in the console.
🌐
ReqBin
reqbin.com › code › javascript › 6onvgezc › javascript-string-length-example
How do I get the length of a string in JavaScript?
December 16, 2022 - To get the length of a string in JavaScript, you can to use the string.length property. The length property specifies how many characters the string contains (the length of an empty string is 0). When determining the length of a string, keep ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-length
JavaScript string.length - GeeksforGeeks
July 11, 2025 - The string.length is a property in JS which is used to find the length of a given string.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › length
JavaScript String length - Get Length of String | Vultr Docs
April 10, 2025 - This code declares a string ... of characters in the string, which outputs 13. The JavaScript string length function counts all characters, including spaces and punctuation....
Find elsewhere
🌐
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 maximum length of a String is the maximum value of the Number data type, which in most cases is 253 – 1 (note: the maximum value of the Number data type can vary based on the underlying implementation of the JavaScript standard).
🌐
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.
🌐
Etleap
etleap.com › blog › what-is-the-length-of-a-string
What is the “length” of a string? - Etleap
For example, "😃" has a length ... the properties panel. Finding the length of a string in JavaScript is simple, you use the .length property and that’s it, right?...
🌐
SheCodes
shecodes.io › athena › 54957-what-is-the-length-property-in-javascript
[JavaScript] - What is the .length property in JavaScript? | SheCodes
International Women’s Week Sale 🎉 30% off on all coding workshops ending on March 9th Ending in 5 days Get Deal Get This Deal NOW ... Learn about the .length property in JavaScript and how it works for strings, arrays, and other objects.
Top answer
1 of 3
7

A string is immutable in JavaScript.

a += "somestring" doesn't change the length of a string but makes a new string.

This means there is no "new length", but the length is just part of the definition of the string (more precisely it is stored in the same structure in implementations).

Regarding

for(i=0;i<a.length;i++){ // did you forget the 'var' keyword ?

a not so uncommon practice (if you don't change a) was to optimize it as

for (var i=0, l=a.length; i<l; i++)

in order to avoid the reading of the length but if you compare the performances with modern engines, you'll see this doesn't make the code any faster now.

What you must remember : querying the length of a string is fast because there is no computation. What's a little less fast is building strings (for example with concatenation).

2 of 3
5

Strings are a primitive type. At least that's what the documentation says. But we can access the length of the string as if we are accessing the property of an object(with the dot notation). Which indicates it's an object, Right?

Turns out, whenever we make a call from the string primitive to some property using the dot notation (for example, say length), the Js engine will take this primitive string and wrap it into an equivalent wrapper object, which is a String object. And then, the .length on that String object returns the length.

Interesting thing to note here is, that when we do something like this, our string still stays the same primitive string during all of this. And a temporary object is created to make our string operation work. Once the required property is fetched, this temporary object is deleted from the memory.

Hope this gives some high level understanding.

🌐
Flexiple
flexiple.com › javascript › javascript-string-length
JavaScript String Length property explained | Flexiple Tutorials | JavaScript - Flexiple
March 11, 2022 - This essentially means that the characters in your string are encoded into a 16-bit long binary number before being stored. So whenever the .length property is involved, JavaScript looks up and returns the number of code units occupied by the string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-the-length-of-a-string
JavaScript - Length of a String - GeeksforGeeks
July 23, 2025 - This approach directly utilizes the built-in length property of strings in JavaScript, which returns the number of characters in the given string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
Pads the current string from the end with a given string and returns a new string of the length targetLength.
Top answer
1 of 2
5

(V8 developer here.)

I can see several issues here that can be looked at separately:

1. From a language specification perspective, is something a method or a property?

Intuitively, the distinction is: if you write a function call like obj.method(), then it's a method; if you write obj.property (no ()), then it's a property.

Of course in JavaScript, you could also say that everything is a property, and in case the current value of the property is a function, then that makes it a method. So obj.method gets you a reference to that function, and obj.method() gets and immediately calls it:

Copyvar obj = {};
obj.foo = function() { console.log("function called"); return 42; }
var x = obj.foo();  // A method!
var func = obj.foo;  // A property!
x = func();  // A call!
obj.foo = 42;
obj.foo();  // A TypeError!

2. When it looks like a property access, is it always a direct read/write from/to memory, or might some function get executed under the hood?

The latter. JavaScript itself even provides this capability to objects you can create:

Copyvar obj = {};
Object.defineProperty(obj, "property", {
  get: function() { console.log("getter was called"); return 42; },
  set: function(x) { console.log("setter was called"); }
});
// *Looks* like a pair of property accesses, but will call getter and setter:
obj.property = obj.property + 1;

The key is that users of this obj don't have to care that getters/setters are involved, to them .property looks like a property. This is of course very much intentional: implementation details of obj are abstracted away; you could modify the part of the code that sets up obj and its .property from a plain property to a getter/setter pair or vice versa without having to worry about updating other parts of the code that read/write it.

Some built-in objects rely on this trick, the most common example is arrays' .length: while it's specified to be a property with certain "magic" behavior, the most straightforward way for engines to implement this is to use a getter/setter pair under the hood, where in particular the setter does the work of truncating any extra array elements if you set the length to a smaller value than before.

3. So what does "abc".length do in V8?

It reads a property directly from memory. All strings in V8 always have a length field internally. As commenters have pointed out, JavaScript strings are immutable, so the internal length field is written only once (when the string is created), and then becomes a read-only property.

Of course this is an internal implementation detail. Hypothetically, an engine could use a "C-style" string format internally, and then it would have to use a strlen()-like function to determine a string's length when needed. However, on a managed heap, being able to quickly determine each object's size is generally important for performance, so I'd be surprised if an engine actually made this choice. "Pascal-style" strings, where the length is stored explicitly, are more suitable for JavaScript and similar garbage-collected languages.

So, in particular, I'd say it's fair to assume that reading myString.length in JavaScript is always a very fast operation regardless of the string's length, because it does not iterate the string.

4. What about String.length?

Well, this doesn't have anything to do with strings or their lengths :-)

String is a function (e.g. you can call String(123) to get "123"), and all functions have a length property describing their number of formal parameters:

Copyfunction two_params(a, b) { }
console.log(two_params.length);  // 2

As for whether that's a "simple property" or a getter under the hood: there's no reason to assume that it's not a simple property, but there's also no reason to assume that engines can't internally do whatever they want (so long as there's no observable functional difference) if they think it increases performance or saves memory or simplifies things or improves some other metric they care about :-)
(And engines can and do make use of this freedom, for various forms of "lazy"/on-demand computation, caching, optimization -- there are plenty of internal function calls that you probably wouldn't expect, and on the flip side what you "clearly see" as a function call in the JS source might (or might not!) get inlined or otherwise optimized away. The details change over time, and across different engines.)

2 of 2
3

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.

Also, note that the String.length property gives the actual number of code units in a string, rather than a literal character count. One code unit is 16 bits as defined by UTF-16 (used by JavaScript). However, some special characters use 32 bits which means that in a string containing one of these characters the String.length property might give you a higher character count than the literal number of characters.

Link:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

And also one fact length work very different with string.length from Array.length

Copylet myString = "bluebells";
myString.length = 4;
console.log(myString); //bluebells
console.log(myString.length); //9


//--
let myArr = [5,6,8,2,4,7];
myArr.length = 2;
console.log(myArr); //[5, 6]
console.log(myArr.length); //2
🌐
Flexiple
flexiple.com › javascript › javascript-length
How to use Javascript Length on Arrays and Strings? - Flexiple
March 14, 2022 - And in case the string is empty it returns 0. const xyz = "Hello World!"; console.log(xyz.length); // Output: 12 · While using Javascript length on an array, the function returns the number of elements in the array.
🌐
Medium
medium.com › @amirakhaled2027 › string-length-vs-string-size-in-javascript-393ec8d77a3b
string.length vs string.size in JavaScript | by Amira Khaled | Medium
August 24, 2024 - The string.length property returns the number of characters in a string, including spaces, punctuation marks, and special characters.