Categories
JavaScript Answers

How to format numbers with JavaScript?

Sometimes, we want to format numbers with JavaScript.

In this article, we’ll look at how to format numbers with JavaScript.

How to format numbers with JavaScript?

To format numbers with JavaScript, we call the number toLocaleString method.

For instance, we write

const value = (100000).toLocaleString(undefined, { minimumFractionDigits: 2 });
console.log(value);

to call toLocaleString with { minimumFractionDigits: 2 } to return a number string with the number rounded to 2 decimal places.

Conclusion

To format numbers with JavaScript, we call the number toLocaleString method.

Categories
JavaScript Answers

How to remove multiple elements from array in JavaScript?

Sometimes, we want to remove multiple elements from array in JavaScript.

In this article, we’ll look at how to remove multiple elements from array in JavaScript.

How to remove multiple elements from array in JavaScript?

To remove multiple elements from array in JavaScript, we can use the array filter method.

For instance, we write

let valuesArr = ["v1", "v2", "v3", "v4", "v5"];
const removeValFrom = [0, 2, 4];
valuesArr = valuesArr.filter((value, index) => {
  return removeValFrom.indexOf(index) === -1;
});

to call valueArr.filter with a callback that checks if the index of the element in valuesArr is in the removeValFrom array which has the indexes of the items in valuesArr that we want to remove.

A new array with the items that we want to keep are included, so we assign the array back to valuesArr.

Conclusion

To remove multiple elements from array in JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to call a JavaScript function every 5 seconds continuously?

Sometimes, we want to call a JavaScript function every 5 seconds continuously.

In this article, we’ll look at how to call a JavaScript function every 5 seconds continuously.

How to call a JavaScript function every 5 seconds continuously?

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.

For instance, we write

const interval = setInterval(() => {
  // ...
}, 5000);

clearInterval(interval);

to call setInterval with the callback we want to run and 5000 millisecond period.

Then the callback runs every 5 seconds.

Conclusion

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs.

Categories
JavaScript Answers

How to shorten conditional statements with JavaScript?

Sometimes, we want to shorten conditional statements with JavaScript.

In this article, we’ll look at how to shorten conditional statements with JavaScript.

How to shorten conditional statements with JavaScript?

To shorten conditional statements with JavaScript, we can use the array includes method.

For instance, instead of writing

if (test.type == 1 || test.type == 2 || test.type == 3 || test.type == 4) {
  // do something.
}

We can shorten that to

if ([1, 2, 3, 4].includes(test.type)) {
  // do something
}

We check if at least one of 1, 2, 3, and 4 are set as the value of test.type with

test.type == 1 || test.type == 2 || test.type == 3 || test.type == 4

or

[1, 2, 3, 4].includes(test.type)
Categories
JavaScript Answers

How to add upload progress indicators for fetch?

Sometimes, we want to add upload progress indicators for fetch.

In this article, we’ll look at how to add upload progress indicators for fetch.

How to add upload progress indicators for fetch?

To add upload progress indicators for fetch, we replace fetch with axios.

To install it, we run

npm i axios

Then we use it by writing

const data = await axios.request({
  method: "post",
  url,
  data: myData,
  onUploadProgress: (p) => {
    console.log(p, p.loaded / p.total);
  },
});

in an async function to make a post request.

We add the onUploadProgress method in the object we call axios.request with to get the progress of our request.

p.loaded has the number of bytes and p.total has the total number of bytes for the request that needs to be sent.

Conclusion

To add upload progress indicators for fetch, we replace fetch with axios.