Categories
JavaScript Answers

How to add 0 to number if less than 10 in JavaScript?

Sometimes, we want to add 0 to number if less than 10 in JavaScript.

In this article, we’ll look at how to add 0 to number if less than 10 in JavaScript.

How to add 0 to number if less than 10 in JavaScript?

To add 0 to number if less than 10 in JavaScript, we can call the string padStart method.

For instance, we write

const n1 = (1).toString().padStart(2, "0");
const n2 = (11).toString().padStart(2, "0");

to call padStart with 2 and '0' to prepend '0' to the number convert to a string if it has less than 2 characters.

Therefore, n1 is '01' and n2 is '11'.

Conclusion

To add 0 to number if less than 10 in JavaScript, we can call the string padStart method.

Categories
JavaScript Answers

How to add an onclick function to go to a URL in JavaScript?

Sometimes, we want to add an onclick function to go to a URL in JavaScript.

In this article, we’ll look at how to add an onclick function to go to a URL in JavaScript.

How to add an onclick function to go to a URL in JavaScript?

To add an onclick function to go to a URL in JavaScript, we set window.location to the URL we want to go to on the onclick function.

For instance, we write

window.location = url;

to set window.location to url to navigate to the URL in the url string.

Conclusion

To add an onclick function to go to a URL in JavaScript, we set window.location to the URL we want to go to on the onclick function.

Categories
JavaScript Answers

How to get the difference between two Dates in JavaScript?

Sometimes, we want to get the difference between two Dates in JavaScript.

In this article, we’ll look at how to get the difference between two Dates in JavaScript.

How to get the difference between two Dates in JavaScript?

To get the difference between two Dates in JavaScript, we can subtract 2 dates directly.

For instance, we write

const msMinute = 60 * 1000;
const msDay = 60 * 60 * 24 * 1000;
const a = new Date(2021, 2, 12, 23, 59, 59);
const b = new Date("2022 march 12");

console.log(Math.floor((b - a) / msDay), " full days between");
console.log(Math.floor(((b - a) % msDay) / msMinute), " full minutes between");

to subtract b from a to get the difference of their timestamps in milliseconds.

Then we get the floor of the quotient of their difference divided by msDay to get the days difference.

And then we get the floor of the quotient of (b - a) % msDay) divided by msMinute to get the minute difference between b and a‘s times.

Conclusion

To get the difference between two Dates in JavaScript, we can subtract 2 dates directly.

Categories
JavaScript Answers

How to determine the current line number in JavaScript?

Sometimes, we want to determine the current line number in JavaScript.

In this article, we’ll look at how to determine the current line number in JavaScript.

How to determine the current line number in JavaScript?

To determine the current line number in JavaScript, we can get the lineNumber property from an Error object.

For instance, we write

const thisLine = new Error().lineNumber;

to create a new Error object and get the lineNumber property from it to get the line number of the current line.

Conclusion

To determine the current line number in JavaScript, we can get the lineNumber property from an Error object.

Categories
TypeScript Answers

How to fix Property does not exist on type ‘object’ with TypeScript?

Sometimes, we want to fix Property does not exist on type ‘object’ with TypeScript.

In this article, we’ll look at how to fix Property does not exist on type ‘object’ with TypeScript.

How to fix Property does not exist on type ‘object’ with TypeScript?

To fix Property does not exist on type ‘object’ with TypeScript, we can add the missing property to the type that we set for the variable.

For instance, we write

interface Provider {
  region: string;
  country: string;
  locale: string;
  company: string;
}

let countryProviders: Array<Provider>;
let allProviders: Array<Provider>;

to create the Provider interface with the properties that we expect to have in an object with type Provider and their types.

And then we define variables that has an array type with entries in them being Provider objects.

Conclusion

To fix Property does not exist on type ‘object’ with TypeScript, we can add the missing property to the type that we set for the variable.