How to create javascript

I don’t understand how to write JavaScript on browser so plzz tell me in simple way

There are two ways of using JavaScript to manipulate contents and make our websites more interactive-

  1. JavaScript code written in HTML itself -

[getElementById() is a method that finds an HTML element (with id=“demo”) and changes the element content ( innerHTML ) to “My First JavaScript” ]

The HTML code for the above would be as follows:

<!DOCTYPE html>
<html>
    <title>Javascript Variables</title>
    <body>
        <h2>JavaScript inside HTML</h2>
        <p id="demo"></p>
        <script>document.getElementById("demo").innerHTML = "My First JavaScript";</script>
    </body>
</html>

Save this HTML file and open it in a browser to see how the content is displayed!

Next up,

  1. JavaScript code written in a separate file -

First, write this code in the text editor and save it in a file with a .js extension like myJavaScript.js

document.getElementById(“demo”).innerHTML = “My First JavaScript”;

Next,

Update the HTML code above to embed the .js file created above

The HTML code would be as follows:

<!DOCTYPE html>
<html>
    <title>Javascript Variables</title>
    <body>
        <h2>JavaScript inside HTML</h2>
        <p id="demo"></p>
        <script src="myJavaScript.js"></script>
    </body>
</html>

Save this HTML file and open it in a browser to see how the content is displayed!

So there you have it, two ways of writing and using JavaScript for your projects!

1 Like

Here the id are used for specifying exact line/place where we want to insert .js files in .html files.

1 Like