Categories
JavaScript Answers

How to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method?

Sometimes, we need to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method.

In this article, we’ll look at how to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method.

How to fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method?

To fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method, we should make sure we’re calling toISOString with a Date instance that has a valid date value.

For instance, instead of writing:

const d1 = new Date('undefined').toISOString()

We write:

const d2 = new Date(2022, 1, 1).toISOString()
console.log(d2)

to create a valid Date instance and call toISOString on it.

As a result, we get that d2 is 2022-02-01T08:00:00.000Z.

Conclusion

To fix the ‘RangeError: Invalid time value’ error when calling the JavaScript date’s toISOString method, we should make sure we’re calling toISOString with a Date instance that has a valid date value.

Categories
JavaScript Answers

How to remove numbers from a string using JavaScript?

Sometimes, we want to remove numbers from a string using JavaScript.

In this article, we’ll look at how to remove numbers from a string using JavaScript.

How to remove numbers from a string using JavaScript?

To remove numbers from a string using JavaScript, we can use the JavaScript string’s replace method with a regex.

For instance, we write:

const string = 'All23';
const newString = string.replace(/\d+/g, '')
console.log(newString)

to return a string that has all the numbers in string removed.

We call replace with a regex that matches all digits and replace them with empty strings.

As a result, newString is 'All'.

Conclusion

To remove numbers from a string using JavaScript, we can use the JavaScript string’s replace method with a regex.

Categories
JavaScript Answers

How to programmatically get memory usage in Chrome with JavaScript?

Sometimes, we want to programmatically get memory usage in Chrome with JavaScript.

In this article, we’ll look at how to programmatically get memory usage in Chrome with JavaScript.

How to programmatically get memory usage in Chrome with JavaScript?

To programmatically get memory usage in Chrome with JavaScript, we can use the window.performance.memory property.

For instance, we write:

console.log(window.performance.memory)

to log the current memory usage in Chrome.

As a result, we get something like:

{
  "jsHeapSizeLimit": 2172649472,
  "totalJSHeapSize": 6722851,
  "usedJSHeapSize": 5630251
}

logged.

All the numbers are in bytes.

jsHeapSizeLimit is the maximum size of the heap that is available to the context.

totalJsHeapSize is current size of the JavaScript heap including free space not occupied by any JavaScript objects.

And usedJSHeapSize is the currently active segment of JS heap.

Conclusion

To programmatically get memory usage in Chrome with JavaScript, we can use the window.performance.memory property.

Categories
JavaScript Answers

How to check if an input field has focus with JavaScript?

Sometimes, we want to check if an input field has focus with JavaScript.

In this article, we’ll look at how to check if an input field has focus with JavaScript.

How to check if an input field has focus with JavaScript?

To check if an input field has focus with JavaScript, we can use the document.activeElement property to get the element in focus.

For instance, we write:

<input>

to add an input.

Then we write:

console.log(document.querySelector('input') === document.activeElement)

to check if the input element is focused.

document.activeElement‘s value is the element that’s current in focus.

Conclusion

To check if an input field has focus with JavaScript, we can use the document.activeElement property to get the element in focus.

Categories
JavaScript Answers

How to convert Blob to JSON with the JavaScript File API?

Sometimes, we want to convert Blob to JSON with the JavaScript File API.

In this article, we’ll look at how to convert Blob to JSON with the JavaScript File API.

How to convert Blob to JSON with the JavaScript File API?

To convert Blob to JSON with the JavaScript File API, we can use the FileReader constructor.

For instance, we write:

const b = new Blob([JSON.stringify({
  "test": "toast"
})], {
  type: "application/json"
})
const fr = new FileReader();

fr.onload = (e) => {
  console.log(JSON.parse(e.target.result))
};

fr.readAsText(b);

We create a blob with a JSON string as its content with the Blob constructor.

We set the type to 'application/json' to set the blob type to JSON.

Next, we create a FileReader instance and set the onload property to a function that gets the parsed result from e.target.result.

Then we call JSON.parse to convert the parsed JSON blob string to an object.

Finally, we call readAsText with blob b to read the blob into a string.

Therefore, the console log should log {test: 'toast'}.

Conclusion

To convert Blob to JSON with the JavaScript File API, we can use the FileReader constructor.