The purpose of this class is to introduce to the student:
- What is a function (ES5 only)
- Wat is scope (global, functional, block)
- How to combine variables, loops & functions
FIRST HALF (12.00 - 13.30)
Week 1
In Week 1 we learnt about how data is represented computer programs:
- Variables
- Basic data types (
string,number, etc) - Compound data types (
array,object)
Week 2
In Week 2 we learnt about how to manipulate the flow of execution in computer programs:
- Branching constructs
if..else
- Looping constructs
forfor..offor..indo..while
We also learnt about more ways to work with data using operators:
- Operators (arithmetic, comparison, eg
+,*,&&,||, etc).
Ask students to explain a concept or summerise the last lecture themselves
Functions are a way to organize your code in to re-usable chunks.
People think that computer science is the art of geniuses but the actual reality is the opposite, just many people doing things that build on each other, like a wall of mini stones.
-- Donald Knuth
https://github.com/HackYourFuture/fundamentals/blob/master/fundamentals/functions.md
Same link as Explanation
https://github.com/yash-kapila/HYF-JS1-Week3/tree/master/src
- Reusability: Functions can be grouped together to make a module (or library), and then modules can be imported into your application so you can build awesome apps!
- Abstraction: Hide underlying details about how a piece of functionality works under the hood. You can change how things are implemented within the function without other programmers who use your function worrying aobut the exact details of how it was implemented.
And the same link as Explanation
SECOND HALF (14.00 - 16.00)
Scopes define the visiblity of declarations of variables and functions.
The top level outside all your functions is called the global scope. Values defined in the global scope are accessible from everywhere in the code.
varandfunctiondeclarations are visible with function scope.letandconstdeclarations are visible with block scope.
https://github.com/HackYourFuture/fundamentals/blob/master/fundamentals/scope.md
let villan = "Joker"; // | global scope
// |
function myFunction() { // | | function scope
let hero = "Batman"; // | |
if (hero === "Batman") { // | | | block scope
let coHero = "Robin"; // | | |
console.log(hero); // | | |
console.log(coHero); // | | |
console.log(villan); // | | |
} // | | |
console.log("------") // | |
console.log(hero); // | |
console.log(coHero); // | |
console.log(villan); // | |
} // | |
// |
myFunction(); // |And the same link as Explanation
What happens if we use the same variable name in different scopes?
function myFunction() {
let hero = "Batman";
if (true) {
let hero = "The Flash";
console.log(hero);
}
console.log(hero);
}
myFunction();Same link as Explanation
Special thanks to Jim Cramer, Yash Kapila, David Nudge for most of the content
