How to get the number of bytes of a variable in JavaScript

1 Answer

0 votes
// Get size of the variable (approximation for JavaScript)

const myVar = 86; 

const sizeOfMyVar = new TextEncoder().encode(myVar.toString()).length;
console.log(`Size of myVar: ${sizeOfMyVar} bytes`);



/*
run:

Size of myVar: 2 bytes

*/

 



answered Jun 4, 2025 by avibootz

Related questions

1 answer 90 views
1 answer 97 views
1 answer 191 views
1 answer 128 views
1 answer 158 views
1 answer 67 views
1 answer 145 views
...