Day2

Functions, scope and this keyword

Resouces: Functions & this keyword

Functions

In Javascript every function is a Function object.

Function Declaration

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

function name(parameter1, parameter2, parameter3) {
  // code
}

Inside the function, the arguments (the parameters) behave as local variables. Function parameters - listed inside the parentheses () in the function definition. Function arguments - values received by the function when it is invoked.

##Function Expression Function Expression has the same syntax as a function declaration.

var myFunction = function name(parameter1, parameter2, parameter3) {
    //code
};

Example for function expression

var evenOrOdd = function(num){
if(num%2 == 0){
return "even";
}
else{
return "odd";
}
};
document.write(evenOrOdd(10));

#Anonymous function i.e. function without name is also possible in JS.

var myFunction = function (parameter1, parameter2, parameter3) {
    //code
}

Passing Functions to other Functions

Here, function is passed as a parameter to a function

var studentIDs = [30,10,20,5,100]
studentIds.sort(function(num1,num2){
return num1-num2;
});

document.write(studentIds);

Adding method to a object

Here’s example for it.

var student = {
id: 1,
name:"BOB",
display: function (){
document.write(student.id + "<br/>");
document.write(student.name);
}
};

student.display();
}

Arguments

JS takes as number of parameters

function product(){
var result = 1;
for(var i = 0; i <arguments.length; i++){
result*=arguments[i];
}
return result;
}

document.write(product(3,4,5));

All these arguments can be accessed by using arguments.

this keyword

β€œthis” keyword refers to an object, that object which is executing the current bit of javascript code.

var student = {
id: 1,
name:"BOB",
display: function (){
document.write(student.id + "<br/>");
document.write(student.name);
}
};

student.display();
}

In the above code instead of using student.id and student.name, it can be written as this.id and this.name as this keyword refers to object

var student = {
id: 1,
name:"BOB",
display: function (){
document.write(this.id + "<br/>");
document.write(this.name);
}
};

student.display();
}

Scope

Basically in JavaSCript there are two scopes: Local Scope & Global Scope. In JS, each function creates a new scope.

What scope does?

Scope determines the accessibility of these variables.

Variables defined inside a function are not accessible (visible) from outside the function.

Local Variables

Variables declared within a JavaScript function, cannot be used outside the function.

// code here can NOT use myName

function nameFunction() {
  var myName = "Rutuja";

  //code here can use myName

}
}

Note: Variables used declared inside functions are local to particular functions only, varibles with same name can be used outside functions.

Global Variables

Variables defined outside functions are global variables. All scripts and functions on a web page can access global variables.

var myName = "Rutuja";

//code here can use myName

function nameFunction() {

  //code here can use myName

}

Assigning a value to a variable which is not declared, it will automatically become a global variable. nameFunction();

// code here can use myName

function nameFunction() {
  myName = "Rutuja";
}

In JS, lifetime of variable starts when it is declared and local variables are deleted when the function is completed. In a web browser, global variables are deleted when you close the browser window.

Learn more about JavaScript variables here.

for more info about this topic view link1 and link2