Functions Flashcards
calling a function defined with arguments with empty braces or fewer arguments
will not cause any error
ex.: function greet(person1, person2) {…}
it’s ok to call it greet(); and greet(“Max”); and greet(“Max”, “Kate”);
nested functions
function outer(){ let movie = "Some";
function inner(){ console.log(movie.toUpperCase()); }
inner();
}
- we call outer(), we can’t call inner() directly
- inner() is only called if it is not only declared but also invoked insided outer()
- the variables of outer() are visible in inner()
function expressions (anonymous)
const sum = function (x, y){ return x + y; }
in order to call it:
sum(1, 2);
functions are OBJECTS!!!
function expressions (named)
const sum = function add (x, y){ return x + y; }
in order to call it:
sum(1, 2);
functions are OBJECTS!!!
functions are OBJECTS!!! we can store them in an array
const sum = function (x, y){ return x + y; }
const subtract = function (x, y){ return x - y; }
const ops = [sum, subtract];
if we print it to console - it will just print the descriptions of functions as objects, but we can also call them
ops0 // 3
for(let func of ops){ let result = func(1, 3); } //this will call each function in ops with the same parameters
functions are OBJECTS!!! we can define values of object attributes as functions
const sum = function (x, y){ return x + y; }
const thing = { doSmth: sum }
thing. doSmth(); will execute the sum function
thing. doSmth will just print it
high order functions
- can accept other functions as arguments
function somefun(func){ func(); func(); }
- can return functions
function somefun(min, max){ return function(val){ return val >= min && val <= max; } }
const inRange = somefun(10, 90);
inRange(13); //true
inRange(100); //false
callback function
function passed into another function as an argument and then invoked!!! inside this another function
function somefun(func){ //func is a callback!!! func(); func(); }
callback function can be anonymous
example.
setTimeout(func, 5000) is a built-in browser function that executes the function passed as argument after the specified number of milliseconds
- we can define func separately and name it
function myFunc(){ alert("yes"); }
setTimeout(myFunc, 50000)
- OR ANONYMOUS function directly:
setTimeout(function(){ alert(“yes”);}, 50000);
hoisting
- this will work without error (hoisting, the interpreter finds the function definiton although it is below)
howl();
function howl(){ console.log(“…”);}
- if we assign to var (function expression), then it will follow the rules of hoisting for var
howl();
var howl = function { console.log(“…”);}
this will throw Uncaught TypeError: howl is not a function, because according to the rules of hoisting for var - in this case, howl is undefined. The interpreter rearranges the code like this. So it can not call something undefined
var howl; //undefined var
howl();
howl = function { console.log(“…”);}
BUT. If we do console.log(howl) - it will show undefined. (no error thrown). Because this var is undefined due to hoisting
- If we use in case 2. let or const instead of var - there will be exceptions thrown, also according to the hoisting rules for let and const
Uncaught ReferenceError
forEach
accepts a callback and performs it on every element of the given array
nums.forEach(function(n){ //n represents the element
console.log(n*n);
})
you can also use index of the element in the array as second parameter
nums.forEach(function(num, index){ //n represents the element
console.log(index, num);
})
OR with a separate named function
function triple(num){ console.log(3 * num); }
nums.forEach(triple);
map
creates a new array with the results of calling a callback on every element in the array
const texts = [“text1”, “text2”];
const caps = texts.map(function(text){ return text.toUpperCase(); });
now caps is an array and it has values of texts in upper case
in the function you MUST RETURN value that represents what to do with the original value. Otherwise, map will not work
arrow functions
const square = function(num){ return num*num; }
const square = (num) => { return num*num; }
if you have just one param:
const square = num => { return num*num;}
with zero params:
const square = () => {…;}
arrow functions with implicit returns
if you return something in the function and this is a one liner, no other logic instead of this
const square = num => { return num*num;}
you can say this:
const square = num => ( num*num )
OR EVEN
const square = num => num*num
find
finds and retrieves element from array. Only one! stops after the first one
needs to return true or false
let movies = [“m1 film”, “m2 film”]
const movie = movies.find(movie => { return movie.includes('m1'); } )
const movie = movies.find(movie => (movie.indexOf(‘m1’) === 0; ) )