The purpose of this lesson is to teach you enough javascript so you can use node.js. You should know the basics of html and css for getting the best of this course.
Javascript is a programming language created in 1995 for the use on web browsers and has since become the most used language in Github. In recent years Node.js has emerged as a new technology for powering the servers with javascript, thanks in part to the javascript's engine V8 and a community of passionate developers.
Variables
There are many ways to represent information. You can represent the number of students with an integer, your name with a string (text), etc. But in programs you might want to reuse these so you store them somewhere: in variables.
A variable is a place where you store some information. Imagine that you want to store a hypothetical number of students of Libre University, 80. You would do so like this in javascript:
var students = 80;
We are creating the variable students
with the keyword var
and assigning it the value of 80
.
Now we want to get the number of students for next year, taking into
account that 20 students will graduate and 30 new students will join.
We could do it like this:
var students = 80; var graduating = 20; var joining = 30; var nextyear = students + joining - graduating;
Variable types
Some examples of variables with a comment ( // ...
) naming them:
// Number: var years = 24; // Boolean: var married = true; // String: var address = "Street 24, New York"; // Array (of strings): var friends = ["Mary", "John", "July"]; // Object var friend = { firstname: "Peter", lastname: "Smith" };
To display them you can use the function alert()
and a pop-up will show you the value.
Control structures
The programs normally execute from top to bottom, line by line. However there are few structures in javascript that can change this, one being control structures. The characteristic of them is that they are a single operation followed by a code block delimited by { }
, and that block is the part that gets modified.
if ( condition )
When using the variables mentioned above sometimes you want that a piece of code is executed in one situation or not. For doing this we use the if
clause:
var name = "John"; if (name == "Peter") { alert("Hello Peter"); } if (name == "John") { alert("Hello John"); }
Notice how we use ==
instead of =
. This is because a single equal sign is used to assign a variable, while a double (or triple) equal is used to compare two things.
But what do you do when you also want to do something when the statement is not true? Then you can use the else
clause:
var name = "Peter"; if (name == "Peter") { alert("Hello Peter"); } else { alert("You are not Peter!"); }
We can compare any type of variable. You can also compare numbers and operations like greater than or smaller than like this:
var age = 17; if (age >= 18) { alert("You are allowed to see this page"); } else { alert("This page is only for adults"); }
for ( initial ; condition ; change )
There's another important control structure called for
. It is used when you want to perform an operation many times but you don't want to write the same code over and over again. For example, say Bloody Mary 3 times:
for(var i = 0; i < 3; i++) { alert("Bloody Mary"); }
Note how we put i < 3
to display it three times, since it will be executed when i is 0, when i is 1 and when i is 2. If we had said i <= 3
then we'd also execute the block when i is 3, so it'd be 4 times in total.
.forEach(function(){})
A similar one would be .forEach()
. It is used to loop an array, however we'll see it later since it involves method and callbacks, some more advanced techniques.
Functions
A function is a piece of code that takes an input and returns an output. Let's see a typical example, the function "sum":
function sum (a, b) { return a + b; }
This function accepts two arguments and return the sum of them. To use it you can test this code:
function sum (a, b) { return a + b; } var result = sum(5, 7); alert(result);
Objects and methods
An object is a type of variable that can contain other elements called properties. It is different from an array in that each element has a name. For example, you can have the object person:
var person = { firstname: "John", lastname: "Doe", age: 21 };
To read or write one of the properties of person you can access them using a .
:
var person = { firstname: "Jonh" }; // Oops, we made a typo in firstname. Fix it: person.firstname = "John"; // Display the name of the person alert(person.firstname);
Objects can also store functions. Let's allow for the previous person to say his name:
var person = { firstname: "John", sayHi: function(){ alert("Hi, I'm John"); } }; // It shows "Hi, I'm John" person.sayHi();
this
To access a variable within an object or function, you can use this
keyword. For the last example:
var person = { firstname: "John", sayHiTo: function(to){ var name = this.firstname; // Join the string with the name alert("Hi " + to + ", I'm " + name); } }; // It shows "Hi Peter, I'm John" person.sayHiTo("Peter");
Async
Async...