Week 7 - JS Functions Extra
Week 7 - JS Functions Extra
● Comparison
● Logical operators
● Functions
○ Function declaration and call
○ Arguments and parameters
○ Scope
Comparison
Equal value a == b
Value and data type are equal a === b
Not equal value a != b
Not equal value or data type a !== b
Logical Operators
- Abstraction: hide the internals of the code for end user; [for user]
- Reusability: use the same code multiple times in the program; [for user and developer]
- Modularity: organize and divide the program into sub tasks; [for developer]
Function declaration
function multiplyByTwo(a)
return a * 2;
}
Function call
function multiplyByTwo(a)
return a * 2;
let a = 3;
let b = multiplyByTwo(a);
console.log(b);
Function declaration
const multiplyByThree = function(a)
return a * 3;
}
Function call
const multiplyByThree = function(a)
return a * 3;
let a = 3;
let b = multiplyByThree(a);
console.log(b);
Function declaration and call comparison
function multiplyByTwo(a) const multiplyByThree = function(a)
{ {
return a * 2; return a * 3;
} }
let a = 3; let a = 3;
console.log(b); console.log(b);
Function parameters and arguments
function functionName(parameter)
{ function declaration
//do something
return something;
let b = functionName(argument);
https://www.w3schools.com/js/js_function_parameters.asp
Function arguments
https://www.w3schools.com/js/js_scope.asp
Scope
Global Scope. A variable declared outside a function, have Global Scope. Global
variables can be accessed from anywhere in a JavaScript program.
Block Scope. Let and Const variables declared inside a { } block cannot be
accessed from outside the block.
*variables assigned without var, let and const automatically have a global scope;
https://www.w3schools.com/js/js_scope.asp
Summary
Key takeaways:
• A function is block of code to perform specific task and used for: reusability,
abstraction and modularity
• Remember 2-step function usage: declare and call
• The arguments are passed by value, though object and array arguments can be
changed inside a function
• JavaScript has 3 types of scope: block, function, global
• Visibility of variables differs depending on scope