Autor: 20.07.2023
JavaScript - simple tutorial for beginners
JavaScript is a great choice for beginner programmers. This language has various applications, including website development, web applications, and mobile applications. It is relatively easy to learn, highly versatile, and offers a wide range of employment opportunities. It's definitely worth considering if you're thinking about learning programming seriously.
In this article, we will show you how you can write your first program in JavaScript.
How to run JavaScript code
JavaScript code is executed by an interpreter, which is a special program that analyzes the program's code and runs it. This allows the program to be executed and return the result of its operations. Traditionally, web browsers serve as such interpreters, as they have the capability to run JavaScript code. So, prepare your favorite browser, such as Chrome, Edge, Firefox, etc.
Your first JavaScript code
To write code from this article, you will need an editor. There are many free tools available, such as the highly popular Visual Studio Code. It is a good choice for beginner programmers and beyond.
Step 1 - Creating HTML document
First, create a file with the .html extension in your chosen directory. Open the file in your editor. You can name the file however you like, but it's recommended to name it index.html. Enter the following code into the file:
<!DOCTYPE html>
<html>
<head>
<title>Mój pierwszy skrypt JavaScript</title>
</head>
<body>
<h1>Witaj!</h1>
<script src="script.js"></script>
</body>
</html>
That is the code for a simple web page. At the bottom, you'll find the element <script src="script.js"></script>. This is a reference to the JavaScript code from a separate file that we'll create shortly.
Step 2 - creating JavaScript document
Now let's create a new JavaScript file named "script.js". Place it in the same directory as the HTML file. Open the "script.js" file in your chosen editor. We will now write our first JavaScript code. Enter the following code into the file:
console.log("Hello!");
This line of code will display a brief greeting in the browser's console.
Step 3 - run the code
It's time to see the results of our work! The HTML file is linked to the JavaScript code file, remember this line: <script src="script.js"></script>? Great!
Open the HTML file in a web browser by double-clicking on it or using the browser's "Open File" option. Now, you need to open the browser's console. In most browsers, you can do this by pressing the F12 key. Make sure the "Console" tab is selected. If everything worked correctly, you should see the greeting we defined in the script.js file in the console. In our console, the greeting looks like this:
If you see this output, then congratulations! You have successfully executed your first JavaScript code.
Summary and next steps
The example we presented in the article is just the absolute basics of JavaScript. From this point on, the actual learning process begins. Displaying a greeting in the console is the first and very important step.
If you want to learn programming from scratch and acquire the necessary skills to work as a Junior JavaScript Developer, we recommend checking out theJavaScript Developer Career Path. It includes courses, exercises, code examples, and practical projects that will help you gain the essential programming skills. The path concludes with an exam that grants you the Certification, validating your proficiency in JavaScript.