You can use the Blob to get the string size in bytes.

Examples:

console.info(
  new Blob(['']).size,                             // 4
  new Blob(['']).size,                             // 4
  new Blob(['']).size,                           // 8
  new Blob(['']).size,                           // 8
  new Blob(['I\'m a string']).size,                  // 12

  // from Premasagar correction of Lauri's answer for
  // strings containing lone characters in the surrogate pair range:
  // https://stackoverflow.com/a/39488643/6225838
  new Blob([String.fromCharCode(55555)]).size,       // 3
  new Blob([String.fromCharCode(55555, 57000)]).size // 4 (not 6)
);

Answer from P Roitto on Stack Overflow
🌐
DEV Community
dev.to › rajnishkatharotiya › get-byte-size-of-the-string-in-javascript-20jm
Get Byte size of the string in Javascript - DEV Community
April 12, 2020 - In this series, I’m going to talk about some shortcodes and useful functions of javascript. These snippets can help you to make your development more efficient and faster. Stay tuned till the end to learn something new… 😊 · As we all know a byte is one the unit of digital information, and while development taking care of the size of variables, records and files are a much important task. To do that we have various ways but with these functions, it’s very easy to do. byteSize() snippet will take a string as an input and in the output, it’ll return byte size of a given string.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › string › byte size of string
Calculate the byte size of a JavaScript string - 30 seconds of code
March 9, 2024 - const byteSize = str => new Blob([str]).size; const helloWorldString = 'Hello World'; helloWorldString.length; // 11 byteSize(helloWorldString); // 11 const emojisString = '😀'; emojiString.length; // 2 byteSize(emojisString); // 4 ... Delve deep into the Myers diff algorithm and learn how to calculate the difference between two strings in JavaScript, the way Git does.
Top answer
1 of 16
265

Years passed and nowadays you can do it natively

const textEncoder = new TextEncoder();
console.log(textEncoder.encode('foo').length);

Note that it's not supported by IE (you may use a polyfill for that).

MDN documentation

Standard specifications

2 of 16
109

There is no way to do it in JavaScript natively. (See Riccardo Galli's answer for a modern approach.)


For historical reference or where TextEncoder APIs are still unavailable.

If you know the character encoding, you can calculate it yourself though.

encodeURIComponent assumes UTF-8 as the character encoding, so if you need that encoding, you can do,

function lengthInUtf8Bytes(str) {
  // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
  var m = encodeURIComponent(str).match(/%[89ABab]/g);
  return str.length + (m ? m.length : 0);
}

This should work because of the way UTF-8 encodes multi-byte sequences. The first encoded byte always starts with either a high bit of zero for a single byte sequence, or a byte whose first hex digit is C, D, E, or F. The second and subsequent bytes are the ones whose first two bits are 10. Those are the extra bytes you want to count in UTF-8.

The table in wikipedia makes it clearer

Bits        Last code point Byte 1          Byte 2          Byte 3
  7         U+007F          0xxxxxxx
 11         U+07FF          110xxxxx        10xxxxxx
 16         U+FFFF          1110xxxx        10xxxxxx        10xxxxxx
...

If instead you need to understand the page encoding, you can use this trick:

function lengthInPageEncoding(s) {
  var a = document.createElement('A');
  a.href = '#' + s;
  var sEncoded = a.href;
  sEncoded = sEncoded.substring(sEncoded.indexOf('#') + 1);
  var m = sEncoded.match(/%[0-9a-f]{2}/g);
  return sEncoded.length - (m ? m.length * 2 : 0);
}
🌐
W3Resource
w3resource.com › javascript-exercises › fundamental › javascript-fundamental-exercise-13.php
JavaScript fundamental (ES6 Syntax): Convert the length of a given string in bytes - w3resource
Write a JavaScript program to convert a given string's length to bytes. Convert a given string to a Blob Object. Use Blob.size to get the length of the string in bytes.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-length-of-a-string-in-bytes-in-javascript
How to Get the Length of a String in Bytes in JavaScript ? - GeeksforGeeks
July 23, 2025 - The Blob interface creates a binary large object from the string and returns its byte size. It’s useful for handling file-like data in web browsers. This approach is straightforward and works well with browser-based JavaScript.
🌐
LabEx
labex.io › tutorials › calculating-string-byte-size-28182
Calculating String Byte Size in JavaScript | LabEx
In the Node.js console, let's test our byteSize function with various strings: ... Notice that with the mixed character types, especially with non-ASCII characters like Chinese characters and emojis, the byte size is larger than the character count. This is important to understand when working with data that might contain international characters or special symbols, as it affects storage requirements and data transfer sizes. ... This will return you to the regular terminal prompt. Now let's create a JavaScript file to implement our byte size function in a more practical way.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › length
String: length - JavaScript | MDN
However, a string with this length needs 16384TiB of storage, which cannot fit in any reasonable device's memory, so implementations tend to lower the threshold, which allows the string's length to be conveniently stored in a 32-bit integer.
🌐
CloudHadoop
cloudhadoop.com › string-size-bytes-javascript
How to get string size in bytes in javascript examples
programming coding blog examples and tutorials on Java,Javascript,Typescript,Unix Linux Commands
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-length-of-a-string-in-bytes-in-javascript
How to get the length of a string in bytes in JavaScript?
Steps to get the length of a string in bytes using buffer API are as follows ? Step 1 ? Create a function which is taking a string as input · Step 2 ? Now we pass the string into the Buffer.from() method and store it in a variable · Step 3 ? Use the .length method to find the size of string ...
🌐
Bitstack
blog.bitsrc.io › how-big-is-a-javascript-string-ef2af3d222e6
How big is a JavaScript string?
September 7, 2022 - On the other hand, astral code points are 4 bytes long. The conversion logic is explained in details here. ... Some JavaScript packages are available for common operations like retrieving a string’s size in bytes or slicing it bytewise (see ...
🌐
GitHub
gist.github.com › lovasoa › 11357947
Compute the length in bytes of a javascript string, when encoded in UTF8 · GitHub
Compute the length in bytes of a javascript string, when encoded in UTF8 - UTF8byteLength.js
🌐
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 - string.size measures the memory footprint of a string in bytes. Use string.length when you need to know the number of characters in a string, regardless of encoding. Use string.size when you need to know the memory footprint of a string, especially when dealing with different character sets or encodings. Understanding the distinction between string.length and string.size helps us, JavaScript developers, to handle strings effectively and optimize their code for memory efficiency.
🌐
W3Schools
w3schools.com › nodejs › met_buffer_bytelength.asp
W3Schools.com
The Buffer.byteLength() method returns the length of a specified string object, in bytes. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report ...
Top answer
1 of 3
15

It would be a lot simpler to work out the length yourself rather than to interpret the results of encodeURI().

/**
 * Count bytes in a string's UTF-8 representation.
 *
 * @param   string
 * @return  int
 */
function getByteLen(normal_val) {
    // Force string type
    normal_val = String(normal_val);

    var byteLen = 0;
    for (var i = 0; i < normal_val.length; i++) {
        var c = normal_val.charCodeAt(i);
        byteLen += (c & 0xf800) == 0xd800 ? 2 :  // Code point is half of a surrogate pair
                   c < (1 <<  7) ? 1 :
                   c < (1 << 11) ? 2 : 3;
    }
    return byteLen;
}

JavaScript implementations may use either UCS-2 or UTF-16 to represent strings.

UCS-2 only supports Unicode code points up to U+FFFF, and such Unicode characters occupy 1, 2, or 3 bytes in their UTF-8 representation. This is not too tricky to handle.

However, as @Mac points out, UTF-16 surrogate pairs are a tricky special case. UTF-16 extends UCS-2 by adding support for code points U+10000 to U+10FFFF, which UTF-16 encodes using a pair of code points. The first code point of such a pair (called the "high surrogate") is in the range D800 to DBFF; it should always be followed by another code point (called the "low surrogate") is in the range DC00 to DFFF. Observe that the UTF-8 representation of any character in the range U+10000 to U+10FFFF would take 4 bytes. Therefore, any surrogate pair in UTF-16 would translate to a 4-byte UTF-8 representation. Or, we could say that whenever we encounter half of a surrogate pair (i.e., a code point is in the range from D800 to DFFF), just add two bytes to the UTF-8 length.

2 of 3
8

My 2 cents

  • Please do not abbreviate words, choose short words or acronyms ( Len -> Length )
  • Please lower camel case ( normal_val -> normalValue )
  • Consider using spartan conventions ( s -> generic string )
  • new Array() is considered old skool, consider var byte_pieces = []
  • You are using byte_pieces to track the bytes just to get the length, you could have just kept track of the length, this would be more efficient
  • I am not sure what abnormal pieces would be here:

if(normal_pieces[i] && normal_pieces[i] != '')

  • You check again for these here, probably not needed:

if(encoded_pieces[i] && encoded_pieces[i] != '')

  • You could just do return byte_pieces.length instead of
// Array length is the number of bytes in string
var byte_length = byte_pieces.length;

return byte_length;

All that together, I would counter propose something like this:

function getByteCount( s )
{
  var count = 0, stringLength = s.length, i;
  s = String( s || "" );
  for( i = 0 ; i < stringLength ; i++ )
  {
    var partCount = encodeURI( s[i] ).split("%").length;
    count += partCount==1?1:partCount-1;
  }
  return count;
}
getByteCount("i  js");
getByteCount("abc def");

You could get the sum by using .reduce(), I leave that as an exercise to the reader.

Finally, if you are truly concerned about performance, there are some very fancy performant js libraries out there.

🌐
npm
npmjs.com › package › string-byte-length
string-byte-length - npm
Get the UTF-8 byte length of a string. Fastest available library in JavaScript.
      » npm install string-byte-length
    
Published   Mar 29, 2025
Version   3.0.1
Author   ehmicky
🌐
Jasonbutz
jasonbutz.info › 2018 › 05 › javascript-unicode-bytes
String Length & Bytes In JavaScript | jasonbutz.info
May 29, 2018 - But if you run 'ü'.length it will return the string’s length as 1. Unicode characters can appear as a single character but be made up of multiple bytes of data. Usually, this isn’t a big deal if you just need the length of a string, but if you actually need the size in bytes of a string ...
🌐
GitHub
gist.github.com › mathiasbynens › 1010324
UTF-8 byte counter in 49 bytes · GitHub
... //count UTF-8 bytes of a string function byteLengthOf(s){ //assuming the String is UCS-2(aka UTF-16) encoded var n=0; for(var i=0,l=s.length; i<l; i++){ var hi=s.charCodeAt(i); if(hi<0x0080){ //[0x0000, 0x007F] n+=1; }else if(hi<0x0800){ ...
🌐
QuickRef.ME
quickref.me › home › how to get the length of a string in bytes in javascript - quickref.me
How to get the length of a string in bytes in JavaScript - QuickRef.ME
In this Article we will go through how to get the length of a string in bytes only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
🌐
GitHub
github.com › ehmicky › string-byte-length
GitHub - ehmicky/string-byte-length: Get the UTF-8 byte length of a string.
Get the UTF-8 byte length of a string. Fastest available library in JavaScript.
Author   ehmicky