Categories
JavaScript Answers

How to add named function parameters with JavaScript?

Sometimes, we want to add named function parameters with JavaScript.

In this article, we’ll look at how to add named function parameters with JavaScript.

How to add named function parameters with JavaScript?

To add named function parameters with JavaScript, we can make a function accept an object as a parameter.

Then we can destructure the object’s properties into variables.

For instance, we write:

const callApi = ({
  url,
  callback,
  query = {},
  body = {}
}) => {

}

callApi({
  url: "/api/..",
  callback: (x => console.log(x)),
  body: {
    a: 2
  }
})

We have the callApi function that takes an object with the url, callback, query and body properties.

We set the default value of query and body to an empty when it’s not set.

Then we call it with an object with those properties set.

Conclusion

To add named function parameters with JavaScript, we can make a function accept an object as a parameter.

Then we can destructure the object’s properties into variables.

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 Get the Last Value Inserted into a JavaScript Set?

To get the last value inserted into a JavaScript set, we can spread the set entries into an array.

Then we can call the pop method on the returned array to return the last entry inserted into the set.

For instance, we can write:

const a = new Set([1, 2, 3]);
a.add(10);
const lastValue = [...a].pop();
console.log(lastValue)

We create a new set with the Set constructor.

Then we call add to add a new entry into the set.

Next, we spread a set into an array and then call pop to return the last element from the set and remove that entry from the array.

The returned item is assigned to lastValue.

Therefore, from the console log, we see that lastValue is 10.