Categories
JavaScript Answers

How to remove leading zeros from a number in JavaScript?

Sometimes, we want to remove leading zeros from a number in JavaScript.

In this article, we’ll look at how to remove leading zeros from a number in JavaScript.

How to remove leading zeros from a number in JavaScript?

To remove leading zeros from a number in JavaScript, we can use the Number function.

For instance, we write

const numString = "065";
number = Number(numString);
console.log(number);

to call Number with numString to return the number converted from numString.

The leading zeroes in the string will be dropped.

Conclusion

To remove leading zeros from a number in JavaScript, we can use the Number function.

Categories
JavaScript Answers

How to remove property for all objects in array with JavaScript?

Sometimes, we want to remove property for all objects in array with JavaScript.

In this article, we’ll look at how to remove property for all objects in array with JavaScript.

How to remove property for all objects in array with JavaScript?

To remove property for all objects in array with JavaScript, we can use the rest syntax.

For instance, we write

const newArray = array.map(
  ({ dropAttr1, dropAttr2, ...keepAttrs }) => keepAttrs
);

to call array.map with a callback that get an object with all the properties we want to keep in keepAttrs.

And then we return that as the value.

Conclusion

To remove property for all objects in array with JavaScript, we can use the rest syntax.

Categories
HTML

How to prevent refresh of page when button inside form clicked?

Sometimes, we want to prevent refresh of page when button inside form clicked.

In this article, we’ll look at how to prevent refresh of page when button inside form clicked.

How to prevent refresh of page when button inside form clicked?

To prevent refresh of page when button inside form clicked, we set the type attribute of the button to button.

For instance, we write

<button type="button">Click</button>

to add a button in the form with the type attribute set to button to prevent refresh of the page when we click on the button.

Conclusion

To prevent refresh of page when button inside form clicked, we set the type attribute of the button to button.

Categories
JavaScript Answers

How to convert an ISO date to the date format yyyy-mm-dd with JavaScript?

To convert an ISO date to the date format yyyy-mm-dd with JavaScript, we can call the string substring method.

For instance, we write

const date = new Date("2022-03-10T02:00:00Z");
const isoDate = date.toISOString().substring(0, 10);

to create a new Date instance.

Then we call toISOString to return a string with the ISO date time.

And then we get the date portion of the string with substring.

Conclusion

To convert an ISO date to the date format yyyy-mm-dd with JavaScript, we can call the string substring method.

Categories
JavaScript Answers

How to rethrow an exception in JavaScript, but preserve the stack?

Sometimes, we want to rethrow an exception in JavaScript, but preserve the stack.

In this article, we’ll look at how to rethrow an exception in JavaScript, but preserve the stack.

How to rethrow an exception in JavaScript, but preserve the stack?

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.

For instance, we write

throw err;

to throw the err Error instance with throw.

Conclusion

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.