Skip to content

Introduction

1. Introduction to JavaScript language

### A. Programs, languages, scripts ...

Before starting: we should not scare us through technical language. For whom start from scratch, these things are the first thing I should know: what is that program, language, scripts...

A program No is more than a series of instructions that we give to a system to do things. In other words, and in the case that concerns us, that is to tell adquio as Make a certain task. Can therefore be something as simple as telling him that he adds two numbers and tells us the result to something as complex how to tell him that he controls a whole building: temperature, doors, lighting... in our case is something quite simple, let's go adquio That when an event occurs, such as a presence detector was activated, do something like, for example, to start a point of light, start the musical thread, etc.

Javascript is an interpreted language, that is, the written program (source code) is read instruction to instruction, translates and runs. That is, it is necessary to translate the program each time it is executed. Who translates the program's instructions? Well, very simple, another program: the translator or interpreter .

B. Javascript....

Write a program, therefore it is simply write instructions for you to execute them adquio, Using a certain language for this. It is like writing in Spanish, you need to know the vocabulary and grammar of the language. In our case we use the language Javascript and you need to know their rules and vocabulary.

C. Variables, data, objects ...

To start using JavaScript, it is necessary to know some basic concepts, we can not start making a house if we do not know that the bricks are there. A program is a list of instructions, but these instructions should be executed on something, if we give an instruction write we must specify what is to be written. It is evident because in the instructions of the program they must also include the data with which to work. For example, a person's name is "John", this word is a data. The price of an apple at € 0,10, this number is another data. These data are not usually used as stored in elements with name called variables. In the previous examples it would use a variable, name, to store "John" or price to store 0.10. If now I say to that you write name adquio Write your content, that is, John. A small example for not getting bored: enters adquio and come to the option of the Script Main Menu, create a new one and give a name, in the text box write this:

let name = 'John';
logger.warn(name);

You have just written a pair of instructions in JavaScript! (Do not worry still about its meaning), as you have given a value to the variable name and then the value of that variable has been written. In the previous example you could also have written:

logger.warn('My name: ' + name);

By means of the symbol + you have concatenated both chains. You have used an operator, something that is not new if you have ever studied mathematics. Operators are symbols used to perform operations with data, are the +, -, /,* symbols, respectively add, subtract, divide and multiply. As you know these operators are used with numerical data: 4 + 5 are 9. These two instructions that you have written could be enclosed in a block with a name . For example IntroduceMySelf() and you would have a function. We could define a function called whattime() that encloses the necessary instructions for the current time to appear in the log. That is to say through the functions we create the orders that we can then give to adquio to act according to our desire.

Up to here you have seen the existing basic elements any programming language, but you will have read that JavaScript is a language that uses objects. And what about objects? Objects are like an extension of the variables, a structure that allows us to group different values ​​and functions names.

A numerical variable can only contain that, a number: 10 or 20 or 2000, a type chain can only save a series of "Acad" characters, "Le18P". An object goes further can store several things at a time. Does it sound weird?. Let's see a very simple example: a rectangle is characterized by the length of its sides and by the way of drawing it.

In a program the rectangle would be assimilated to an object that would have two properties: base and height, and a method: like_draw it. This one like_draw it would be a function that draws rectangles. If a program defines the MyRectangle variable as an object of this type and contains the MyRectangle instruction.

The bottom window of the editor that you have in front is an object with properties and methods such as Log, Warm, Error ... Let's do something with this object that is called Logger, to go to the window of your script and write:

js logger.warn('hello World');
Have you seen what happens? You have used the warn() method of the Logger object to greet. To see the result you must click on the lower button Save and then refresh at the bottom of Log, to the right of it.

The objects are quite more complicated than exhibited here, JavaScript can create them and of course manipulate them. The scripts manipulate the own objects of language and those that we have created.

D. The execution of the scripts.

Usually when you want to run a program just search for the corresponding file and make a simple click of mouse or press the ENTER key. But what about JavaScript programs?

In the examples you have written in the previous sections, you have sent instructions in JavaScript to adquio, you have given them live orders. But this is not the usual way of executing programs in JavaScript.

The normal thing is that the execution is carried out automatically when in an event happens, or when the user decides to launch it from another script, etc.

Changes that adquiop from the outside for their buses, probes, etc. provoke the so-called events that will react in the appropriate way: if you have scheduled something and you have linked to that event.

Those events are those who take advantage of the instructions that we write in JavaScript are executed. Each event can be associated with a function to do something predetermined by us.

For example, when a digital input changes value an event that can be used to make another output activate. Events have the nature of objects, that is, they have methods and properties.

Thus when an event occurs we can know who triggers it, and other properties dependent on each particular event.

But let's continue with the theme of the execution of the programs, let's see what about the program flow. When adquio start reading the script to execute it it does it in sequential order, that is, start with the first instruction, it continues for the second and thus until it reaches the end.

This is what is known as sequential or linear execution. But sometimes it is necessary to skip instructions, for example, build a script that can only enter if another variable contains a value, you must write a function that requests such a value, if it is appropriate continuously execution and if not, the Rest of the code.

Your program has not followed a linear flow, sometimes it will execute that part of your code and others do not. Another fairly common situation: you want your program to travel all the modifiable variables of a device and change your content, you will not write the same code over and over again, the ideal would be to write the instructions and be able to repeat it ...

Any programming language has been solved this matter through the so-called program flow control sentences. They are sentences that allow them to perform conditionally, some steps (conditional) or repeat a series of instructions over and over again (loops). These instructions will see it in subsequent chapters, with examples and tests.