If you don’t know any JavaScript, this is a quick 5-minute course explaining most things you will need to understand.
Full JavaScript Docs
If you need a more in-depth explanation, you can look up anything on developer.mozilla.org, everything except for the DOM.
Semicolons
Semicolons in JavaScript are optional comparing to Java and Groovy, as in JavaScript we have automatic semicolon insertions.
Logging
// Display's word hello in browser console. console.log('hello'); // Show in the console object data from crm const payload = await crmManager.getPayload(); console.log(`This is data from the payload {payload}`);
Variables
To define a variable, use the let
or const
keyword. Don’t use var
as it is a global variable and can conflict with interceptor, app and CRM.
let someString = 'some-string' let someNumber = 69 let someBool = true const array = ['string', 69, true] const object = { key: 'value', 'key-word': 'value', }
Flow control
//if else let i = 0; if (i < 0) { console.log("its negative"); } else if (i > 0) { console.log("its more than 0"); } else { console.log("its 0"); } // while loop let i = 0; while (i < 5) { console.log(i); i++; } // for loop for (let i = 0; i < 5; i++) { console.log(i); } // for of loop const array = [0, 1, 2, 3, 4]; for (const item of array) { console.log(item); }
Functions
There are multiple ways to define functions, with the function
keyword or with arrow functions, also known as lambda functions in other languages.
Arrow function (lambda function):
const fn = (param1, param2) => { console.log(param1, param2); }
Named function:
function fn(param1, param2) { console.log(param1, param2); }
Nameless function assigned to a constant variable:
const fn = function(param1, param2) { console.log(param1, param2); }
All of these functions can be invoked in the same way, like any other function:
fn('Hola', 'pricefx');
Additionally, JavaScript supports default parameter values, which can be used to provide a default value for a parameter if no value is provided when the function is called. Here's an example:
function fn(param1, param2 = 'hello') { console.log(param1, param2); } fn(); // Outputs: undefined, hello
Destructuring
Arrays
// Initialize an array with five elements const array = [1, 2, 3, 4, 5]; // Use array destructuring to assign the first two elements to variables, // and the rest of the elements to a new array called 'rest' const [elem1, elem2, ...rest] = array; // Log the values of the variables to the console console.log(elem1, elem2, rest); // Outputs: 1, 2, [3, 4, 5]
Objects
// Initialize an object with four properties const object = {one: 1, two: 2, three: 3, four: 4}; // Use object destructuring to assign the values of the 'one' and 'two' properties to variables, // and the rest of the properties to a new object called 'rest' const {one, two, ...rest} = object; // Log the values of the variables to the console console.log(one, two, rest); // Outputs: 1, 2, {three: 3, four: 4}
String formatting
use backticks
and ${}
const string = 'pricefx' const formatted = `Hi ${string}` console.log(formatted) // Hi pricefx