Fundamental elements in any programming language, even if we’re talking about scripting, are the loops, functions and predefined objects. In this article we will attempt to explain their operations both from a theoretical and practical point of view, through simple code examples.
JavaScript supports three different types of loops: for, while, and do. If we want to print the sum of the first ten numbers that begin with one, we have to write.
... var nSum = 0; var i; for (i = 1; i <= 10; i++) nSum += i; alert("Sum of the first 10 numbers are: " + nSum); ...
From this simple example we can see that the syntax is identical to that of Java or C. Here’s how to do the same operation using the while… loop.
... var nSum = 0; var i = 0; while (i <= 10) { nSum += i; i++; } alert("Sum of the first 10 numbers are: " + nSum); ...
Finally, using the changes do very little, the only difference from the previous form is that the loop is executed at least once, which does not happen with previous loops.
... var nSum = 0; var i = 0; do { nSum += i; i++; } while (i <= 10); alert("Sum of the first 10 numbers are: " + nSum); ...
Functions are a very important feature of JavaScript, they are the grouping of a set of instructions that are executed on the explicit call. If a function is part of an object it is called a method, such as the alert function that we used before is part of the window object provided by the browser DOM. They are used mainly for three reasons:
- Help speed up the realization of different types of script, once achieved a function, this can be called multiple times without having to rewrite all of it.
- Can greatly enhance the readability of the code, choose a name descriptive of what the function does definitely helps to choose the right one to use.
- Ensures the reusability of code, in fact, a function, you can carry it from one script to another.
Let the function code as simple as possible: SayHello().
... function SayHello() { alert ("Hello to everybody!"); } SayHello(); ...
A most useful function equally simple and could be one that calculates the factorial of a smaller number of eight, chosen so low to avoid encumbering the calculations of the machine.
... function Factorial(nValue) { if (nValue < 0 || nValue > 8) return 0; var nTot = 1; if (nValue == 0) return nTot; do { nTot *= nValue; nValue--; } while (nValue > 1); return nTot; } alert ("Factorial of 5 is: " + Factorial(5)); ...
Note that if the value entered as the argument of the function is zero, return the same one, in fact, the factorial of zero is precisely one.
JavaScript also supports object-oriented programming, the interpreter provides developers of predefined classes that can be used, for example, dates, then let’s see use with a simple script.
... var dtHour = new Date(); alert(dtHour.getHours() + " hours and " + dtHours.getMinutes() + " minutes and " + dtHours.getSeconds() + " seconds"); ...
As you can imagine getHours(), getMinutes() and getSeconds() are three methods of the Date object and can be accessed with the notion point.
In this article we showed three fundamental characteristics of each programming language, in this case a scripting in Java. End of this post was not to be exhaustive about these features, probably occurred at least 3-4 lessons, but I tried to explain their operation in the simplest possible way, because the practical examples are the best way to learn to program.