Close Current Tab in JavaScript

- 1 minute read

Sometimes you need to programmatically close the current tab in JavaScript. You might think to use the window.close method for this, but window.close() does not work by itself in most browsers.

Instead, you can instantaneously open and close a window within the current tab:

const closeTab = () => {
    window.close(``, `_parent`, ``);
};

Then, you can just call the above function as it is: closeTab().

Here is a one-liner variant:

const closeTab = () => window.close(``, `_parent`, ``);

Link to this section Conclusion

Sometimes you need to close the current tab from within your app. Unfortunately, it’s not the most intuitive process.

But by saving this handy one-liner, I haven’t had any issues!

I hope this snippet helped you too.