JavaScript
To run a node environment:
docker run -it node node
>
Strings and Variables
Set a variable test
:
> var test = 'ok';
> console.log(test);
ok
With var
you can overwrite the value:
> var test = 'ok';
> var test = 'ok2';
> console.log(test);
ok2
With const
you cannot as the value remains what it was set initially to:
> const test = 'ok';
> const test = 'ok';
Uncaught SyntaxError: Identifier 'test' has already been declared
String interpolation:
> const name = 'ruan';
> var msg = `my name is ${name}`
> console.log(msg);
my name is ruan
With new line characters:
> var msg = `my name is:\n- ${name}`
> console.log(msg);
my name is:
- ruan
Arrays
Define array:
> var dict = [];
Append dictionary to array:
> dict.push({
key: "name",
value: "ruan"
});
Print the array:
> console.log(dict);
[ { key: 'name', value: 'ruan' } ]
IF Statements
If, else and else if:
> var x = 2;
> if (x == 0) {
console.log('x is 0');
} else if (x > 1) {
console.log('x is more than 1');
} else {
console.log('x is probably 1');
}
x is more than 1
If statement using multiple conditions:
> var event = {'name': 'ruan', 'surname': 'bekker', 'age': 34, 'severity': 'Low', 'skip': false}
> if ((!event.skip && event.name == 'ruan')) {
console.log('true');
}
true
The same as above, but adding OR:
> var event = {'name': 'ruan', 'surname': 'bekker', 'age': 34, 'severity': 'Low', 'skip': false}
> if ((!event.skip && event.name == 'frank') || (!event.skip && event.age == 34)) {
console.log('true');
}
true
Or it can be written like this:
> var event = {'name': 'ruan', 'surname': 'bekker', 'age': 34, 'severity': 'Low', 'skip': false}
> if (!event.skip && (event.name == 'frank') || (event.age == 34)) {
console.log('true');
}
true
Read file contents
> const fs = require('fs');
> const fileName = "/tmp/foo-bar";
> const myText = fs.readFileSync(fileName, "utf-8");
> console.log(myText);
hello