Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

JavaScript is written into an HTML document in 2 ways:

  • Using: <script type="text/JavaScript"> Where you would embed your code here </script>
  • Or referencing it kind of like an external CSS: <script type="text/JavaScript" src="myJavaScript.js"></script>

Below is a very simple HTML Skeleton with our JavaScript embedded using the first method.

Example <html>
<body>
<h3>My First JavaScript Code</h3>
<script type="text/JavaScript">
document.write("<p>Hello!</p>");
</script>
</body>
</html>

Below is how the above code would look in a browser.

Result

My First JavaScript Code

Hello!

First, let's break down document.write("<p>Hello!</p>") JavaScript is a Object Based Programming Language. So, in the example above document is the object. In JavaScript, an object followed by a . , such as document. , is an indication that a method or an property will follow. (Also, some programmers often refer to properties as attributes.)

The write here is actually one of the document's methods. Methods generally have arguments, and in this case, the <p>Hello!</p> is the argument.

Now let's understand an object's property.

Example <html>
<body>
<h3>JavaScript Object Properties</h3>
<p>
<script type="text/JavaScript">
document.write(document.location);
</script>
</p>
</body>
</html>

Result

JavaScript Object Properties

http://www.jaredbdrake.com/javascriptdevelopment/javascript1.php

In this example, you can see that we use the document's property, document.location, as an argument for the method, document.write. The document.location is equal to , but since it was in the method document.write, it actually wrote that to the DOM so that we could see it.

References

I've attached my favorite JavaScript and jQuery book. I would encourage you to read it as well.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    khanAca18 Dec. 5, 2016, 12:05 a.m.

    Mabye you should teach the user the language the way they do it on Khan Academy and then teach them how to put it in webpages.

  • User

    reindeer04 April 19, 2016, 3:14 p.m.

    Give use acess to src code and a code runner

  • User

    mgymnast March 11, 2014, 12:09 p.m.

    It doesn't teach you much. It should have more examples where the thing we are learning is highlighted so I know what to loo at. Needs some more work.



Advertisement