What is JavaScript and how it works?

JavaScript Javascript is a computer language or programming code used on a website so that the website becomes more interactive and dynamic. JavaScript is popular on the internet and can work on most popular web browsers like Google Chrome, Internet Explorer (IE), Mozilla Firefox, Netscape and Opera. JavaScript code can be inserted in a web page using the SCRIPT tag.

JavaScript is a type of client side programming language. The use of JavaScript code on a website is optional, meaning it doesn't always have to be. However, modern websites and blogs are almost all of them using javascript code even a little.

JavaScript History

As mentioned earlier, javascript is a programming language developed by Netscape. The design process takes ten days. The first name of javascript is Mocha, then Mona, Live script, then javascript.

Initially the Javascript programming language that was created in September 1995 was only used by Netscape, its function was limited as it is today. But over time, JavaScript continued to be developed to the extent it is today.

Initially in 1996 javascript was called ECMAScript. This continued until 1998. In 1998 ECMAScript 2 and 1999 appeared ECMAScript 3.

ECMAScript continues to be developed until finally it becomes the existing JavaScript. Javascript is almost used by 92 percent of websites. JavaScript can be used for many purposes, for example to create rollover effects in both images and text, and the important thing is to create AJAX. JavaScript is the language used for AJAX.

Variable

Writing variables in standard JavaScript does not have a data type. All values can be stored on all variables. Variables can be specified by let (block level variable), var (function level variable) or const (irreversible variable).

JavaScript Writing

Variables in JavaScript can be called using the var keyword

var x;
var y = 2;

JavaScript code is usually written in the form of a function that is placed in the <head> tag that is opened with the tag <script type = "text / javascript">.

<script type = "text / javascript">
alert ("Hello World!");
</ script>


JavaScript code can also be placed in a separate file with the extension .js (short for JavaScript). To call the JavaScript code in the file itself, at the beginning of the <head> the .js file name in question must be determined first using the code example as follows:

<script type = "text / javascript" src = "cabang.js"></ script>

Script in head

This script will be executed when called (usually in the form of a function) or called based on a trigger at certain events. Putting the script in the head will ensure the script is loaded before being called.

<html>
<head>
<script type="text/javascript">
...
</script>
</head>
</html>

Body script

This script is executed when the page loads up to the <body> section. When placing a script in the <body> section, it means that between the contents and JavaScript are made into one part.

<html>
<head>
</head>
<body>
<script type="text/javascript">
...
</script>
</body>
</html>

The number of JavaScript in <head> and <body> placed in the document is unlimited.

External script

Sometimes there are people who want to run the same JavaScript several times on different pages, but don't want to be busy if they have to rewrite the desired script on each page. Then JavaScript can be written on file externally. So, between HTML and JavaScript documents are separated, then the file is called from an HTML document. The JavaScript file is saved with the .js extension.

JavaScript: js / file .js document.write ("this message appears when the page is loaded");

To use external JavaScript (.js), the attribute "src" is used in the <script> tag on the HTML page.

<html>
<head>
</head>
<body>
<script src = "file .js"></script>
<p> The above script is in the "file .js" (external) file </ p>
</body>
</html>

Benefits, Functions and Roles of JavaScript

As mentioned above, javascript functions to make a web page more interactive and dynamic. The use of javascript code is very needed for websites that are oriented to user-experience. However, the use of this javascript code will not be optimal if it is not accompanied by the use of CSS because javascript will not be separated from html.

The three programming languages ​​are very closely related. If I compare a website to a home, then the html code is everything that builds and is in the house (walls, poles, roofs, foundations, family rooms, rooms, kitchens, household appliances, etc.). Then, the CSS code is the regulator and decorator of the house (layout, color and so on). And the role of javascript can be ‘helpers’ css code so that the settings and decorations are better with additional knick-knacks for example. You can also manipulate html code with a specific name / identity. This manipulation can be hiding, replacing and even adding new html elements.

Without javascript, when performing certain actions, for example clicking on a web page, the results or responses from the results of those clicks must be loaded on another page. This is of course very troublesome because not every click action produces large / large results. Sometimes the result of an action is only a sentence "You have successfully entered data", for example.

Different from the others, this Javascript is client-side. That is, all the processes carried out by this javascript will be carried out by the client (web visitor). If the others are server-side, the process is run by the server. The client only sends a request and the request is processed on the server and the results are sent back to the client via a web browser.

What is the use of JavaScript?

Javascript is a program in the form of a script, which will be run by an interpreter that has been embedded into a web browser, so that the web browser can enter the Javascript program. Javascript programs are entered into HTML documents with tags marked with <Script ...> and ending with </ script> the same as tags in html (<> ... </>).

The JavaScript function is one of the most beautiful parts of Javascript. As a functional language. Javascript implements first class functions that can be stored in variables, returned by other functions, and sent as arguments for other functions. Implementation of very flexible functions like this opens up many opportunities for developers to write code that not only runs well, but is also very elegant and beautiful.

A JavaScript function can wrap one or many commands. Every time we call a function, the commands in the function are executed. In general the function is used for reuse of code (code reuse) and information storage (information hiding). The implementation of first class functions also allows us to use functions as units that can be combined, like a lego. Support for object-oriented programming also means that we can use functions to provide certain behaviors from an object.

With javascript, the HTML writing technique can be done in two ways, namely by creating a javascript program to produce HTML documents or by creating HTML documents as usual, then if there is a javascript program, then we add the javascript program as an insert.