// This is a single-line comment
// Are you ready for more?
var myName = "Glavin"; // Note that you can also put comments after your code statements
var myAge = 20;
// ^ CamelCase is the common form to write variable names in JavaScript
/*
This is a multi-line comment.
This is very helpful,
especially when you like to talk a lot.
*/
function whoami(name, ageInYears) {
// Let's do some arithmetic.
var ageInDays = 365 * ageInYears; // Yes, this math is total BS. Let me have this one :)
// String concatenation is easy in JS!
// Notice how ageInDays is a Number and I can simply + it with a String.
return "My name is "+name+" and I am "+ageInDays+" days old!";
}
/* Invoke a function.
Arguments are myName and myAge.
Inside the function they are mapped arguments to local variables, parameters.
Argument -> Parameter
myName -> name
myAge -> ageInYears
*/
var result = whoami(myName, myAge);
console.log(result);
/*
`result` variable is equal to
"My name is Glavin and I am 7300 days old!"
*/
fs.readFile('test.txt', function (err, data) {
if (err) return console.error(err);
db.insert({
fileName: 'test.txt',
fileContent: data
}, function (err, result) {
if (err) return console.error(err);
smtp.send({
to: 'test@test.com',
subject: 'test',
body: 'This is a test.'
}, function (err) {
if (err) return console.error(err);
console.log("Success email sent.");
});
});
});
fs.readFile('test.txt')
.then(function (data) {
return db.insert({
fileName: 'test.txt',
fileContent: data
});
})
.then(function () {
return smtp.send({
to: 'test@test.com',
subject: 'test',
body: 'This is a test.'
});
})
.then(function () {
console.log("Success email sent.");
}, function (err) {
console.error(err);
});
To Web-Developers
// Get the <button> element with the class 'continue'
// and change its HTML to 'Next Step...'
$( "button.continue" ).html( "Next Step..." );
// Show the #banner-message element that is hidden with `display:none`
// in its CSS when any button in `#button-container` is clicked.
var hiddenBox = $( "#banner-message" );
$( "#button-container button" ).on( "click", function( event ) {
hiddenBox.show();
});
// Require `fs` module
var fs = require("fs");
var fileName = "foo.txt";
// Reading from File System
fs.readFile(fileName, "utf8", function(error, data) {
console.log(data);
});
// Writing to File System
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
// Watch file change events
fs.watch(fileName, {
persistent: true
}, function(event, filename) {
console.log(event + " event occurred on " + filename);
});