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
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.

Categories
JavaScript Answers

How to show a pop-up print dialog box using JavaScript?

Sometimes, we want to show a pop-up print dialog box using JavaScript.

In this article, we’ll look at how to show a pop-up print dialog box using JavaScript.

How to show a pop-up print dialog box using JavaScript?

To show a pop-up print dialog box using JavaScript, we can use the window.print method.

For instance, we write

window.print();  

to open the print dialog box of the browser.

Conclusion

To show a pop-up print dialog box using JavaScript, we can use the window.print method.