JavaScript Chapter 2 - Writing And Using Javascript
Tags: javascript, beginners, tutorial
Published: 2021-05-15 08:58:54

Welcome to the basic javascript tutorial. This is a javascript tutorial chapter 2, where in part 1 we discussed [understanding and introducing javascript](https://dev.to/dzun_n/javascript-chapter-1-understanding-and-introduction-of-javascript-36hb). 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 <head> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gajwpg6705n0d6it29qb.png "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

```javascript
document.getElementById("nama").innerHTML = "Nama Saya Dzun";
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rone285c9igdw65pujcy.png) 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckqiuhd5art8q4njcw2r.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uexs5garqouwv7f9dxk.png)

So the syntax of Javascript is called by:
```javascript
<script type="text/javascript" src="javascript.js"></script>
```
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