JavaScript #4 – First Script & Operators

Today, we are going to write the first script that will run in a browser. In the previous article from this series, we learn what is console and how they can help us to make our code better, clearer and bug-free. Today, we write the first script.

So, open your code editor and write basic structure of HTML. At the end insert<script> tag. Inside we put our code. Our first app must be better than typical Hello World from other tutorials. We are going to make simple, procedural calculator. Before we send result from calculations to the web page elements, I think you have to learn how to manipulate DOM. So, this time result appears on the console.

Firstly, you need to understand how to execute the script and write output information to the console. It’s very simple. When the script has loaded, it runs automatically (in future we learn how to run the script after page loads fully). Meanwhile, write this code

and run the website. Open the console and check result. As you can see, “Hello, World!”. By writing this we refer to a console object and his method log. What are objects and methods I teach you in future, but now please think about it as a collection of functions and procedures. By calling log we send information to our console. Information in brackets are parameters of methods or functions. In this case, the parameter is information, that we want to notice in a console.

Other methods (functions of objects – collections of functions and properties) are:

So, now we can write first math calculations in the console.

as result of above, we see 6.

Now, you know how to output something. It’s time to learn how to get information from users. You can do this in many ways, one of simplest it’s to get information from a pop-up box with a text box.

Write above code and run it. When you type your name or nickname in the pop-up, you see them in the console.

Great, now yo know how to get and use information from users. At present, we can write a calculator.

That’s all! Our simple calculator is ready to use. Save this code and run the site. Now, you are able to write some other mathematical equations like exponentiation of number to the power of two or three.




Leave a Reply

Your email address will not be published. Required fields are marked *