Let's define some words and some syntax rules here. You don't necessarily need to memorize everything, just read it and you can come back here later if you need to.
The most basic thing is the instructions. What separates the instructions is the semicolon.
var x = 5 + 7;
var theText = "Go help who needs you ;)";
There are two instructions above. These are expressions that tell the computer to perform an operation. In this case, assign the value of 5 + 7 to the variable x (it will do the mathematical operation first and simply assign 12) and assign this text to the variable theText.Note that I put semicolons and parentheses in the String, but as they are inside the quotation marks, they will not have their normal function, they will just be text.Another important thing: I could have written it like this:
var x=5+7;Var theText="Go help who needs you ;)";
The computer would understand the same way. The spaces and separation by lines help us, humans, to be able to read the program more easily. The content we assign to the second variable is different from that of the first. The first contains an “integer” and the second a “String”, a simple sequence of characters. A text.Now a function:
biggerThanFive = function (number) {
if (number > 5) {
return true;
}
return false;
}
A function, as I said before, is a little machine inside the machine that is the entire program. You call this function whenever you need to carry out that sequence of instructions. This is good, because this way you don't have to repeat that code a lot of times within the program. Each time you need that operation, you call the function, and you can pass a different input, which we call a parameter, each time.This specific function that I wrote above takes the parameter we call “number” and, through a conditional “if” statement, answers the question with yes or no: is the input greater than 5?In computer language, in fact, it responds as “true” or “false”. True or false.So if I write:
var x = 7;
If ( biggerThanFive(x) ) {
window.console.log("Well, it’s bigger than five.");
} else {
window.console.log("No, no, it’s not.");
}
It will respond according to the value of x. That's it for now. As for loops, I'll come back to them soon, when I'm going to talk about another type of loop that exists in addition to the “while” loop: the “for” loop.