In the first week we learned the basic on Javascript and Html5. We learned about variables, use strict, functions, parameters and more.
Variables are space in the computer memory that we create so we can store information. We can reuse this space over and over again until there is no more connection left between our application and the variable.
Example:
<code>
// creating a empty variable with the name "mVar"
var mVar = "";
// assigning a value to the "mVar" variable.
mVar = "Christian Soler";
// displaying the variable
alert("My name is " + mVar);
</code>
This code snippet will display the message on screen: My name is Christian Soler.
In the first line, we are creating a variable; which creates an empty space in memory. Now we can use this space to store pretty much any type of value we need. In the second line of code, we are storing the string "Christian Soler" inside the mVar variable. Finally, in the third line, we combine the string "My name is " with the variable mVar and display it on the screen.
Function are code blocks that perform a task and may or may not return a value. Functions can be call later when is need it. The syntax of a function start with the "function" keywords, follow by the function name "mFunc" . Then we add a pair of parentheses, "()" and a pair of curly braces "{}". Within the curly braces, we insert the piece of code we want to execute.
Example:
<code>
function mFunc(){
// code to execute
alert("Inside the mFunc function!");
}
</code>
Now we can execute mFunc function from anywhere in our javascript file like this:
<code>
// telling the function to execute.
mFunc();
</code>
This code snippet will display the message on screen: Inside the mFunc function!
NOTE: Another way to write this function is:
<code>
var mFunc = function(){
// code to execute
alert("Inside the mFunc function!");
}
</code>
This is the most used way on writing functions.
Parameters are a way on passing data into a function, where we can do something with the data. We write parameters within the parentheses of a function. We can have as many parameters as we need.
Example:
<code>
// same function as the above one, but with some changes.
var mFunc = function (firstName, lastName){
// variable
var mName = firstName + " " + lastName;
// code to execute
alert("My name is " + mName);
}
</code>
Now, we can call the function like this:
mFunc("Christian", "Soler");
Or from a HTML tag:
<code>
<button onclick="mFunc ('Christian', 'Soler');">click me</button>
</code>
This code snippet will display the message on screen: My name is Christian Soler.
And that is all for week one.