JavaScript introduction for beginners

JavaScript language is intended to run in the browser along with HTML and CSS. But, if these languages ​are created to layout the structure of the site, then JavaScript allows you to "make web pages alive" - make them responsive to user actions or show dynamism (for example, changing pictures in a block or beautiful smoothly drop-down menus).

How to run JavaScript

JavaScript code is written directly into the HTML page inside the script tag. This tag can be placed anywhere on the page. Let’s see an example:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>This is the title</title> <script> we write JavaScript code here </script> </head> <body> This is the main content of the page. </body> </html>

Let's write the first program

Let's write our first JavaScript program. Here it is:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My first program</title> <script> alert('Hello, world!'); </script> </head> <body> my first program </body> </html>

Copy the above code and insert it in an HTML file. Open this file in a browser and you will see a dialog box with text.

Let's analyze the program code

Let's analyze the code of the program we wrote before. The first concept you need to learn is functions. Functions allow you to perform certain actions. In our example, there is a function alert() that displays text on the screen as a dialog box.

The function consists of a name (in our case it is alert), followed by parentheses. In these parentheses, you should write function parameters. In our case, the parameter is the text that is displayed on the screen.

In our case, the function has one parameter, but there can be functions that take multiple parameters. In this case, these parameters are written separated by commas.

Note

In the future, I will not describe how to connect JavaScript, but just write JavaScript code, meaning that you write it in script tags. Taking this note in account, our program will look like this:

alert('Hello, world!');

Practical tasks

Make a file with your first program. Output some text to the screen with it.

enru