If you want to redirect to another page with JavaScript, you can use the window.location object. This object is part of the window global object and has a property called href. The href property is used to get or set the URL of the current page.

You can set the href property to a new value to navigate to a different page. For example, if you wanted to redirect to the Google home page, you would set the href property like this:

window.location.href = 'https://www.google.com';`

This will cause the current page to be replaced with the Google home page. If you want to redirect to a new page without replacing the current page, you can use the window.location.replace() method instead of setting the href property directly.

For example, this will also redirect to the Google home page:

window.location.replace('https://www.google.com');

The difference is that replace() will not keep the current page in the browser history, so if the user clicks back they will not return to your site. In most cases, it is better to just set the href property directly since this gives users more control over their browsing experience.

Another example of redirect

You can redirect the user to a different page with pure JS by changing the global property window.location.

Here’s how it’s done:

window.location.replace("https://learn.coderslang.com");

This code sample redirects the user to the website https://learn.coderslang.com and it’s an equivalent of the HTTP redirect.

If you’re looking for the behaviour that’s similar to clicking a link, you should update the property window.location.href

window.location.href = "https://learn.coderslang.com";

Whatever you choose the user will end up redirected to a new web page.