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 173 views
1 answer 178 views
1 answer 274 views
1 answer 202 views
1 answer 252 views
1 answer 148 views
1 answer 227 views
...