JAVASCRIPT VARIABLE BASICS

How to declare a variable ?

There are 2 ways to declare a variable :

  • const : variable won't change its value and can't be modified
  • let; variable can change its value

A good practice is to declare a variable at the top of their scope = top of function node in IIOT

// 'start' can't be declared again nor modified
const start = Date.now();

// 'elapsedTime' cannot be declared again
let elapsedTime = 0;
// but its value can changed after
elapsedTime = Date.now() - start;

Objects

object declaration : an object is a kind of variable, so you have to use « const » or « let »

Declare Empty Object :

let myObject = {};

Declare an Object with element :

//4 elements in this example
const myFruits = {Name: 'tomato', Weight: 4, Price: 3, Price_Unit: 'Euro'}

You can get an element of an object using '.'

//put the value of element "Name" from the Object "myFruits"
const test = myFruits.Name 
//result test = 'tomato'

VTB / node red fact !

A payload from a message used in VTB is an object and each element represent a tag

//example
msg.payload = { 
    'tag1' : X, 
    'tag2' : Y,
    'tag3' : Z,    
};

That's why to reach the tag1 from the payload we do :

const getTag1 = msg.payload.tag1

Other notation :

// For example we have to use this notation if the tag name contain a space !
const getTag1 = msg.payload["tag1"]

Array

An Array is an object

Declare Empty Array :

let myArray = [];

Declare an Array with elements :

//5 elements in this example
const Braincube =  ['Connection','Structuration','Visualization','Analyze','Improve']

You can get an element of an array using the index ( start at 0 ! )

//get the first element 
const FirstStep = Braincube[0]
//result FirstStep = Connection

Was this article helpful?

Powered by Zendesk