| title | logoImg | theme | transition | highlightTheme | slideNumber | enableMenu | enableChalkboard | autoSlide |
|---|---|---|---|---|---|---|---|---|
Functions |
night |
slide |
dracula |
true |
false |
false |
0 |
"A block of reusable code." {.fragment .current-only }
:::block {.fragment .current-only } There are two steps to this:
- creating the function
- using it / calling it.
:::
"functions are first-class citizens, which means they are treated like any other variable. This is where JavaScript gets it's best features." {.fragment .current-only}
:::block {.fragment .current-only } As a first class citizen, functions can be used as:
- callbacks in other function,
- creating asynchronous operations,
- preform recursion for iterative operations
:::
What a function declaration looks like:
function add(param1, param2){
return param1 + param2;
};
add(1, 2) //return a value of 3Has the following Features:
Block Syntax, with {} at the begining and the end of the function and usually followed by a ; {.fragment .current-only data-code-focus=1-3}
- Function keyword
- Name of function,
add - Parameters
(param1, param2), which act as variables inside the functions definition. {.fragment .current-only data-code-focus=1-1}
Return keyword, ends the function and the result is returned {.fragment .current-only data-code-focus=2-2}
add(1, 2) //return a value of 3Calling the Function:
We pass values, (1,2), into the arguements
{.fragment .current-only data-code-focus=1-1}
Values of arguements can be any type of variable, and because functions are treated like variable, you use can pass them into a arguement which is known as a callback. {.fragment .current-only data-code-focus=1-1}
AAhoisted = "undeclared, hoisted";
var AAA = "declared, not hoisted, var doesn't matter as much, let/const still better";
function functionDeclaration() {
console.log(`\n Is DECLARATION hoisted: ${global.functionEXPRESSION} \n`);
}
var functionEXPRESSION = function () {
console.log(`\n Is EXPRESSION hoisted: ${global.functionEXPRESSION} \n`);
}
// Log the global object
console.log(global)
console.log("function DECLARATION are: ", global.functionDeclaration);
console.log("function EXPRESSION are: ", global.functionEXPRESSION);
// Predict the output code
functionDeclaration();
functionEXPRESSION(); These functions, variables and hoisting work differently in the node.js runtime environment! {.fragment .current-only data-code-focus=1-8}
Will a function DECLARATION be hoisted in node and added to the global object?{.fragment data-code-focus=3-5}
Will a function EXPRESSION be hoisted in node and added to the global object?{.fragment data-code-focus=6-8}
Log the global object and find out.{.fragment .current-only data-code-focus=9-12}
AAhoisted = "undeclared, hoisted";
var AAA = "declared, hoisted, dontUseVar";
function functionDeclaration(params) {
console.log(`\n Is Declaration hoisted: ${window.functionDeclaration} \n`);
}
var functionEXPRESSION = function () {
console.log(`\n Is EXPRESSION hoisted: ${window.functionEXPRESSION} \n`);
}
// Log the global object
console.log(window)
console.log("function DECLARATION are: ", window.functionDeclaration);
console.log("function EXPRESSION are: ", window.functionEXPRESSION);
// Predict the output code
functionDeclaration();
functionEXPRESSION(); These functions work differently in the browser ! {.fragment .current-only data-code-focus=1-8}
Will a function DECLARATION be hoisted in browser and added to the global object?{.fragment data-code-focus=3-5}
Will a function EXPRESSION be hoisted in browser and added to the global object?{.fragment data-code-focus=6-8}
Log the global object and find out.{.fragment .current-only data-code-focus=9-12}
Follow me on Social:
@HansOnCoding
:::block {.fragment .current-only }
:::
@HansUxDev
:::block {.fragment .current-only }
:::