Nest all content into snippets
This commit is contained in:
22
snippets/js/s/byte-size.md
Normal file
22
snippets/js/s/byte-size.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
title: Byte size of string
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [string]
|
||||
cover: mountain-lake-cottage-2
|
||||
dateModified: 2020-10-18T23:04:45+03:00
|
||||
---
|
||||
|
||||
Returns the length of a string in bytes.
|
||||
|
||||
- Convert a given string to a [`Blob` Object](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
|
||||
- Use `Blob.size` to get the length of the string in bytes.
|
||||
|
||||
```js
|
||||
const byteSize = str => new Blob([str]).size;
|
||||
```
|
||||
|
||||
```js
|
||||
byteSize('😀'); // 4
|
||||
byteSize('Hello World'); // 11
|
||||
```
|
||||
Reference in New Issue
Block a user