Javascript (JS) is a dynamic computer programming language. It is embedded or integrated into an HTML

Javascript was first known as Mocha, and then called Livescript, but Netscape changed its name to Javascript because of the popularity of Java at the time. It debuted in Netscape 2.0 in 1995 under the name Livescript. Many people are still confused Java and Javascript is one. But in reality, these two languages are completely different, both conceptual and design.

What is Javascript?

It is defined as follows:

  • a scripting language.
  • a lightweight programming language.
  • an interpreteted language (that is, execution scripts without preliminaries)
  • often embedded directly into HTML page.
  • designed to add interactivity to HTML pages.
  • ..

Why use Javascript?

It is one of three languages used by all web developers: HTML, CSS, JS.

Advantage.

  • Interact less with the server.
  • Immediate feedback to customers.
  • Compatible with increased users.
  • Richer interface.

Disadvantages.

We can not think of JavaScript as a full-fledged programming language. It lacks the following important features:

  • Client-side JavaScript does not allow reading and writing of files for security reasons.
  • It is not used for network applications because there is no support.
  • It does not have any multi-threading or multi-processor capabilities.

Javascript structure?

Rule

Similar to other languages, Javascript also has syntax rules like:

  • Uppercase, lowercase.
  • Long pairs of open and close symbols such as {}, ().
  • Use extra white space, or tabs, which makes it easy to read or edit script files.
  • Use comment lines to make notes about the script’s function and creation time.

Grammar

Javascript is a set of statements that are made up of keywords, operators, and identifiers sorted in the order that it interpreter is in most browsers can understand. The statement can be long, but most are not too complicated, even if set programmers use another programming language.

Javascript position in the web page.

There are three ways to embed Javascript into HTML:

  • Embed the Javascript into the HTML file using the <script> </script> tag.
  • Write it in a separate .js file and link to the HTML file using the link tag.
  • Write it right in the HTML tag

Write JS in the same HTML. using the <script> tag.

In the HTML page, the JS code must be placed in the <script> </script> tag.

EX:javascrip-demo-1

Put the script in <head></head>

Below is a script placed in the <head> tag. The <script></script> tag is a function that replaces text.

EX:javascript-demo-2

Put script in <body> </ body>

Below is a script placed in the <body> tag. The <script> </ script> tag is a function that replaces text similar to the example above.

EX:javascript-demo-3

Put script in <footer></footer>

Same as put in <head></head> and <body></body>

Write JS in a separate file.

The Script can be written in an external file.

We write the script in a separate file when the same code is used in multiple places, this file has the .js extension.

To embed this file into HTML, we set the path and filename in the src (source) attribute of the <script> tag:

EX:javascript-demo-4

The main.js file is written as follows:

EX:
javascript-demo-5

Advantages of placing JS code in a separate file:

  • Splitting HTML clearly with JS code.
  • Easy to read, easy to upgrade, easy to manage.
  • Accelerate page loads.

Write javascript right in the HTML tag

Call the function right in the HTML tag. For this we can write the js function in the <script> </ script> tag in the HTML file, or we can also write the function in a separate .js file and then call it.

EX:
javascript-demo-6-1

How it works?

  • Browser load page.
  • Browser checks the JS code on the web is yes or no.
  • If yes, the browser will forward the JS code to the interpreter.
  • Interpreter process and execute JS code.
  • The code may affect the components of the web page.
  • Browser shows all web content.

Hope it’s useful for you!