DEV Community

Cover image for JavaScript Chapter 2 - Writing And Using Javascript
Dzun N
Dzun N

Posted on • Edited on

JavaScript Chapter 2 - Writing And Using Javascript

Welcome to the basic javascript tutorial. This is a javascript tutorial chapter 2, where in part 1 we discussed understanding and introducing javascript. So that it is not crisp, just enter the material.

HOW TO WRITE JAVASCRIPT

We start with writing good and correct javascript syntax. And as for the basic knowledge of javascript that we must master.

  • Saved with the .js extension
  • The javascript syntax that is inserted in the HTML page is written in the <script> tag
  • JavaScript syntax can be placed in the <head> or <body> tag
  • Element Javascript is case sensitive
  • At the end of each line is closed with a semicolon.

JavaScript syntax can be used in 2 ways. Namely by inserting it on the HTML page directly, and another by saving the javascript file with the .js extension and then including it on the HTML page. To be more specific, we will discuss one by one.

USING JAVASCRIPT INTERNAL

Writing javascript internally is by directly inserting javascript syntax in HTML. If you use this method, you have to put the javascript syntax between the opening <script> tag and the closing </script> tag. And put it in the

tag, and it could also be in the <body> tag. For example, create an HTML file to find out how javascript works / writes internally. It can be an html file or a php file. Because javascript is able to run between the two. Here I just created an html file as an example. Create an html file with an .html extension here I use the name belajar1.html

belajar1.html

Look at the example above. there is a div element which we give the id "name". Then we give it a touch of Javascript with the following command

document.getElementById("nama").innerHTML = "Nama Saya Dzun";
Enter fullscreen mode Exit fullscreen mode

Document is a mandatory syntax in javascript, and the getElementById function functions to order an element that has an id that matches the content in the getElementById parameter itself. Writing big and small must be considered. Because Javascript is case sensitive and innerHTML functions to write HTML. So in full we are calling for writing "My name is Dzun" in the element with the id "name". If run in a web browser the results are as follows:

image

In writing JavaScript it can also be done externally, meaning that the script from JavaScript is written on another page from Index.html as in writing CSS. example:

index.html

image

So the syntax of Javascript is called by:

<script type="text/javascript" src="javascript.js"></script>
Enter fullscreen mode Exit fullscreen mode

And the result is the same as before.

CONCLUSION
We can use 2 methods of writing Javascript, namely internal and external. Internal method, namely by placing the Javascript script in the html file. The External method is by creating our own javascript file and connecting the html file with javascript

Top comments (1)

Collapse
 
habeeb_developer profile image
Habeeb Mohamed • Edited

Nice tut, waiting for part 3