Categories
JavaScript Answers

How to use regular expression for formatting numbers with thousands separators in JavaScript?

Sometimes, we want to use regular expression for formatting numbers with thousands separators in JavaScript.

In this article, we’ll look at how to use regular expression for formatting numbers with thousands separators in JavaScript.

How to use regular expression for formatting numbers with thousands separators in JavaScript?

To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace method.

For instance, we write

const result = subject.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

to replace every groups of 3 digits with the groups plus a comma after it.

Conclusion

To use regular expression for formatting numbers with thousands separators in JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to add zeros to the beginning of a string (max length 4 chars) with JavaScript?

Sometimes, we want to add zeros to the beginning of a string (max length 4 chars) with JavaScript.

In this article, we’ll look at how to add zeros to the beginning of a string (max length 4 chars) with JavaScript.

How to add zeros to the beginning of a string (max length 4 chars) with JavaScript?

To add zeros to the beginning of a string (max length 4 chars) with JavaScript, we use the string padStart method.

For instance, we write

const padded = numberStr.padStart(4, "0");

to call padStart with 4 and '0' to return a string with the numberStr value prepended with 0’s until its length is 4.

Conclusion

To add zeros to the beginning of a string (max length 4 chars) with JavaScript, we use the string padStart method.

Categories
JavaScript Answers

How to make a JSON call to an URL with JavaScript?

Sometimes, we want to make a JSON call to an URL with JavaScript.

In this article, we’ll look at how to make a JSON call to an URL with JavaScript.

How to make a JSON call to an URL with JavaScript?

To make a JSON call to an URL with JavaScript, we use fetch.

For instance, we write

const getJSON = async (url) => {
  const response = await fetch(url);
  return response.json();
};

const data = await getJSON("https://yesno.wtf/api");
console.log(data);

to define the getJSON function.

In it, we call fetch with url to make a get request to url.

Then we get the response from the returned promise with await.

We get the JSON response body by calling json.

Then we call getJSON and get the response body with await.

Conclusion

To make a JSON call to an URL with JavaScript, we use fetch.

Categories
JavaScript Answers

How to set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript?

Sometimes, we want to set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript.

In this article, we’ll look at how to set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript.

How to set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript?

To set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript, we call fullcalendar with an object with the initial date.

For instance, we write

$("#calendar").fullCalendar({
  year: 2022,
  month: 4,
  date: 25,
});

to select the calendar element and then call fullCalendar with an object with the initial year, month, and date values.

Conclusion

To set full calendar to a specific start date when it’s initialized for the 1st time with JavaScript, we call fullcalendar with an object with the initial date.

Categories
JavaScript Answers

How to create a hidden field in JavaScript?

Sometimes, we want to create a hidden field in JavaScript.

In this article, we’ll look at how to create a hidden field in JavaScript.

How to create a hidden field in JavaScript?

To create a hidden field in JavaScript, we call createElement.

For instance, we write

const input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "name");
input.setAttribute("value", "value");

document.body.appendChild(input);

to create an input with createElement.

Then we call setAttribute to set the type, name, and value attribute values of the input.

We set the type attribute to 'hidden' to make it hidden

Next we call appendChild to append the input to the body element as its last child.

Conclusion

To create a hidden field in JavaScript, we call createElement.