Change the Title of the Document with JavaScript | Plantpot

Change the Title of the Document with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to change the title of the document with JavaScript.

The document.title property

In JavaScript, the document.title property is used when you want to change the title of a web page.

Here's how to get the title of the document.

let docTitle = document.title;

If you want to change the title, do as follows.

document.title = "newTitle";
Sponsored links

Sample Code

Below is sample code to change the title of the web page.

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>oldTitle</title>
  </head>
  <body>
    <script>
      console.log(document.title); // "oldTitle"
      document.title = "newTitle";
      console.log(document.title); // "newTitle"
    </script>
  </body>
</html>