Categories
JavaScript Answers

How to add 2 weeks to a JavaScript date?

Sometimes, we want to add 2 weeks to a JavaScript date.

In this article, we’ll look at how to add 2 weeks to a JavaScript date.

How to add 2 weeks to a JavaScript date?

To add 2 weeks to a JavaScript date, we use the Date constructor.

For instance, we write

const date = new Date();
date.setDate(date.getDate() + 14);

to create a date object.

Then we call getDate to get the current date of the month.

Next, we call setDate with the current date of the month plus 14 to set the date to the date of the month 14 days from today.

Conclusion

To add 2 weeks to a JavaScript date, we use the Date constructor.

Categories
JavaScript Answers

How to calculate date from today date to 7 days before with JavaScript?

Sometimes, we want to calculate date from today date to 7 days before with JavaScript.

In this article, we’ll look at how to calculate date from today date to 7 days before with JavaScript.

How to calculate date from today date to 7 days before with JavaScript?

To calculate date from today date to 7 days before with JavaScript, we use the Date constructor.

For instance, we write

const days = 7;
const date = new Date();
const last = new Date(date.getTime() - days * 24 * 60 * 60 * 1000);

to create a new date object.

And then we call getTime to get its timestamp in milliseconds.

Then we subtract that by 7 days after converting it to milliseconds.

Finally we put that in the Date constructor to create a new date 7 days before today.

Conclusion

To calculate date from today date to 7 days before with JavaScript, we use the Date constructor.

Categories
JavaScript Answers

How to get the current location of an iframe with JavaScript?

Sometimes, we want to get the current location of an iframe with JavaScript.

In this article, we’ll look at how to get the current location of an iframe with JavaScript.

How to get the current location of an iframe with JavaScript?

To get the current location of an iframe with JavaScript, we use the contentWindow.location.href property.

For instance, we write

const url = document.getElementById("iframeID").contentWindow.location.href;

to select the iframe with getElementById.

And then we use its contentWindow.location.href property to get its current URL.

Conclusion

To get the current location of an iframe with JavaScript, we use the contentWindow.location.href property.

Categories
JavaScript Answers

How to return values in JavaScript?

Sometimes, we want to return values in JavaScript.

In this article, we’ll look at how to return values in JavaScript.

How to return values in JavaScript?

To return values in JavaScript, we use the return statement.

For instance, we write

const myFunction = (value1, value2, value3) => {
  return { val2: value2, val3: value3 };
};

to define the myFunction function.

It takes 3 parameters.

And we put value2 and value3 in an object and return the object.

Conclusion

To return values in JavaScript, we use the return statement.

Categories
React Answers

How to clean up memory leaks on an unmounted component in React Hooks?

Sometimes, we want to clean up memory leaks on an unmounted component in React Hooks.

In this article, we’ll look at how to clean up memory leaks on an unmounted component in React Hooks.

How to clean up memory leaks on an unmounted component in React Hooks?

To clean up memory leaks on an unmounted component in React Hooks, we use the useMountedState hook from the react-use package.

To install it, we run

npm i react-use

Then in our component, we add

const isMounted = useMountedState();

useEffect(() => {
  const asyncAction = executeAsyncAction();

  asyncAction.then((result) => {
    if (isMounted) {
      // ...
    }
  });
}, []);

to call the useMountedState hook to return a boolean to indicate if the component is mounted.

And then we check isMounted in the useEffect callback before we do anything.

Conclusion

To clean up memory leaks on an unmounted component in React Hooks, we use the useMountedState hook from the react-use package.