Categories
JavaScript Answers

How to check if the user can go back in browser history or not with JavaScript?

Sometimes, we want to check if the user can go back in browser history or not with JavaScript.

In this article, we’ll look at how to check if the user can go back in browser history or not with JavaScript.

How to check if the user can go back in browser history or not with JavaScript?

To check if the user can go back in browser history or not with JavaScript, we can call history.back or history.go.

For instance, we write

history.back();

or

history.go(-1);

to go back to the previous page.

Is there’s no previous page, they’ll do nothing.

Conclusion

To check if the user can go back in browser history or not with JavaScript, we can call history.back or history.go.

Categories
JavaScript Answers

How to get the time zone name in JavaScript?

Sometimes, we want to get the time zone name in JavaScript.

In this article, we’ll look at how to get the time zone name in JavaScript.

How to get the time zone name in JavaScript?

To get the time zone name in JavaScript, we can use the DateTimeFormat constructor.

For instance, we write

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

to call resolvedOptions on the DateTimeFormat object and get the timeZone property to get the device’s current time zone.

Conclusion

To get the time zone name in JavaScript, we can use the DateTimeFormat constructor.

Categories
JavaScript Answers

How to transform object to array with Lodash?

Sometimes, we want to transform object to array with Lodash.

In this article, we’ll look at how to transform object to array with Lodash.

How to transform object to array with Lodash?

To transform object to array with Lodash, w e can use the Object.entries method.

For instance, we write

const arr = Object.entries(obj).map(([key, value]) => ({ key, value }));

to get each property of obj as a key-value pair array.

And then we put the key and value into their own array with map to create an array with the key and value properties.

Conclusion

To transform object to array with Lodash, w e can use the Object.entries method.

Categories
JavaScript Answers

How to detect rotation of Android phone in the browser with JavaScript?

Sometimes, we want to detect rotation of Android phone in the browser with JavaScript.

In this article, we’ll look at how to detect rotation of Android phone in the browser with JavaScript.

How to detect rotation of Android phone in the browser with JavaScript?

To detect rotation of Android phone in the browser with JavaScript, we can listen for the orientationchange event.

For instance, we write

const supportsOrientationChange = "onorientationchange" in window;
constorientationEvent = supportsOrientationChange
  ? "orientationchange"
  : "resize";

window.addEventListener(
  orientationEvent,
  () => {
    console.log(window.orientation, screen.width);
  },
  false
);

to get the event name by checking "onorientationchange" in window.

If onorientationchange is available that means we can listen for the orientationchange event.

Otherwise, we listen for the resize event.

Then we call addEventListener to listen to either event and get the orientation and screen’s width with screen.width.

Conclusion

To detect rotation of Android phone in the browser with JavaScript, we can listen for the orientationchange event.

Categories
JavaScript Answers

How to get difference in months between two dates in JavaScript?

Sometimes, we want to get difference in months between two dates in JavaScript.

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

How to get difference in months between two dates in JavaScript?

To get difference in months between two dates in JavaScript, we subtract the months from the 2 dates.

Then we add back the number of years difference converted to months.

For instance, we write

const monthDiff = (dateFrom, dateTo) => {
  return (
    dateTo.getMonth() -
    dateFrom.getMonth() +
    12 * (dateTo.getFullYear() - dateFrom.getFullYear())
  );
};

to get the monthDiff function.

In it, we call getMonth to get the months.

We get the difference with

dateTo.getMonth() - dateFrom.getMonth()

And we add the years difference with

12 * (dateTo.getFullYear() - dateFrom.getFullYear()

Conclusion

To get difference in months between two dates in JavaScript, we subtract the months from the 2 dates.

Then we add back the number of years difference converted to months.