JavaScript, News and Language Recommendations
The language for scripting in
Note: Although language is standard, some global objects, such as Console are not accessible from scripts.
Some notions about the latest versions of JS
The scripting engine of
Arrow functions
In
It could in turn classify the arrow functions in two classes, below examples of equivalence in traditional notation and arrow.
- Immediate result, which only execute a statement and return that same value. Do not use keys.
// Function in Traditional Syntax
function suma(a, b) {
return a + b;
}
// Syntax arrow Abbreviated Function, assigning a variable
const Addition = (a, b) => a + b;
// Syntax arrow Abbreviated Function, used as a parameter
FunctionThatUsesAddition((a,b) => a + b);
- In a more traditional sense, including several lines and sentences. They have to include keys to delimit their body.
// Function in Traditional Syntax
function ActivateClimate() {
const tExterior = devices.read('PROBES', 'EXTERIOR');
if(tExterior > 19) {
devices.write('CLIMATE', 'ONOFF', true);
} else {
logger.debug('The climate do not need to turn on');
}
return devices.read('CLIMATE', 'ONOFF');
}
// Arrow function Syntax
const ActivateClimate = () => {
const tExterior = devices.read('PROBES', 'EXTERIOR');
if(tExterior > 19) {
devices.write('CLIMATE', 'ONOFF', true);
} else {
logger.debug('The climate do not need to turn on');
}
return devices.read('CLIMATE', 'ONOFF');
}
Note: When the function is exactly receiving a parameter, the parentheses can be omitted, for example: text => logger.debug (text) is a valid arrow function.
Var, let and const
IN
The variables declared with Let andConst only exist in the block that are defined (ie, enclosed by keys { and }). The rules on variables defined with VAR are more complex and escape the intention of this document.
The difference between LET and CONST is that the "variables" assigned with Const can not change value.
const demonstration = (parameter) => {
if(parameter !== null) {
const big = parameter > 1000;
let small = parameter < 10;
if(big) {
big = parameter * 1000; // -> Error by reassigning constant
} else if (small) {
small = small / 10; // OK
}
}
// large and small do not exist here, if they had been defined as Var would exist at this point
logger.debug('Big is', big); // -> Error: big is undefined
}
Comparisons (==, !=, === y !==)
It is recommended to always use the comparators of three symbols (=== y !==). These verify that the value is exactly the same. The most lax versions != And == Try to convert data types to make flexible comparisons, and may result in unexpected errors, for example:
1 === '1' // false
1 == '1' // true
0 === [] // false
0 == [] // true
//Nevertheless... Boolean(0) -> false and Boolean([]) -> true
Error handling (exceptions)
Despite the name, script errors can happen in normal system operation. For example, an error would be to try to write or read about a variable that is out of service. The errors serve to notify the programmer that has occurred a situation that would have to be contemplated differently.
If an error is not handled (that is, it is not captured and specified what to do in case it happens) the script will end prematurely, without executing the lines after the error within the same block.
Unmanaged errors are registered in the script log and generate an alert. In this way you can know that an unexpected circumstance has not been handled correctly.
The usual way to handle errors is putting the code that can throw errors in a Try ... Catch structure:
let reading;
try {
// Code that can generate errors
reading = devices.read('variable', 'problematic');
// If the previous line launches an error, the following will never be executed:
logger.debug('I have been able to read the problematic variable', reading);
} catch(error) {
// Code that runs when an error occurs
logger.warn('Problem reading the problematic variable', error);
}
// What is outside the structure continues to run normally, that is why it is important to leave any variable needed in a consistent state if you want to continue operating after a Try ... Catch block
Async/await
Some methods available within scripts are incompatible with the use of Async / Await (for exampledevices.read() o alerts.create()).
Therefore, it is recommended not to use this feature in the scripts of Promises o async/await.
As a general rule, the code that follows a lineAwait will launch an error when calling one of the internal methods of
Values falsy/truty (Certain or false)
In some cases, values are required that are TRUE orfalse. However, by the nature of soft timing of language, values that are not strictly one of those two are also accepted.
In these cases, it is important to have clear the equivalences that the language will produce, to avoid giving for "a certain" a value that is not.
In this sense, only the following values are equivalent to false:
- false
- null
- undefined (In some cases, the value returned when accessing positions or non-existent properties. It can also be explicitly assigned)
- 0
- NaN (Not a Number, result of performing arithmetic operations without formal solution)
- "" o '' (Empty text, does not include apparently empty texts like " " o "\n" that only contain spaces, Tabs or non-visible characters)
The rest of values will take true value (equals true), including the following (only by pointing some that can lead to confusion):
- "0" (Text containing a zero letter
0, do not confuse with number 0). Pto convert a number into representation text atnumbervalue, put before a+Sum symbol, for example:+" 123 "Returns the numeric value123 - {} (Empty object, without any property)
- [] (List/Empty Array). To evaluate if a list is empty you can access its
lengthproperty that returns an integer with the size of the list - " " (Text with a space)
- "false" (Again, the text strings can lead to deception)