i decided not to learn the git basics now, i need to concentrate more on completing the javascript tutorial on codeacademy.com, this is more important.
so javascript again
so learning about functions now,
how to define a function in javascript
syntax here
var nameoffunction = function(parameters){
...
};
ex:
var dividebythree = function(number){
var val = number / 3;
console.log(val);
};
//calling a function
dividebythree(9);
Here in the syntax, the keyword function tells the computer it is a function not something else. ***Remember to end the function } with a semicolon;
When we call a function we don't always want to print stuff, sometimes we want to return something.
So we use 'return' keyword which ends the function and returns the value that came out of the function
ex:
var fuc = function(parameter){
return par;
};
here the o/p of the fuc function is value of 'par'.
any global variable can be used inside a function, but it is not a good habit to define many global variables.
**Math.random() function will return a value between 0 and 1
doing a mini project in the training course
** to check the variable type in javascript , use typeof() function
ex:
if (typeof(x) !== 'number') {
return "wrong";
}
No comments:
Post a Comment