JS Functions Flashcards
how create Function declaration?
For declare function we use keyword ‘functions’ functions’name, list of parameters, and functions body
function sum(a,b) {
return a+b
}
console.log(sum(2,3));
funcName start whit verb
When we call func she execute code inside the body/this is invocation/
parameters used for declare the func
arguments used for call the func
// Function declaration: starts with “function”
function isNil(value) {
return value == null;
}
What returning value of func ‘result’?
function say(mes) {
console.log(mes);
}
let result = say(‘Hello!’)
console.log(‘Result:’, result);
Undefined
// function say return Hello!
// console.log(‘Result:’, result); return Result: undefined
What is answer?
const compere = (a, b) => {
return a === b ? “equal” : a > b ? a : b;
};
console.log(compere(30, 30));
equal
we have func who compere the numbers
What is answer?
const say1 = (mes) => {
return !mes ? console.log(“Don`t have massage”) : console.log(mes);
};
say1(“”);
say1(“Hello!”);
Don`t have massage
Hello!
What is answer?
function add() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
};
console.log(add(1,2,3));
console.log(add(1,2,3)); //6
Is possible to use declaration func before declare?
The function declaration variable is hoisted up to the top of the current scope, which means that the function can be invoked before the declaration
Is functions are first-class citizens in JavaScript?
Functions are first-class citizens in JavaScript. In other words, you can treat functions like values of other types.
-Storing functions in variables
-Passing a function to another function
-Returning functions from functions
Storing functions in variables?
function add(a,b) {
return a+ b
}
let sum = add
// we have two ways to execute func
console.log(add(1,2)); /3
console.log(sum(2,2)); /4
Passing a function an argument to another function
function add(a,b) {
return a+ b
}
let sum = add
function average(a,b, fn) {
return fn(a,b)/2
}
let result1 = average(2,3, sum)
console.log(result1);
we use func add= sum like argument for func average
console.log(result1); 2,5
Returning functions from functions
function compereBy(propName) {
return function (a, b) {
let x = a[propName];
let y = b[propName];
if (x > y) {
return 1;
} else if (x < y) {
return -1;
} else {
return 0;
}
};
};
let products = [
{
name:’phone’,
price:699
},
{
name:’Protector’,
price:9
},
{
name:’Memory’,
price:20
},
]
console.log(‘Prod sort by name’);
products.sort(compereBy(‘name’))
console.table(products);
console.log(‘Prod sort by price’);
products.sort(compereBy(‘price’))
console.table(products);
console.log(‘Prod sort by name’);
products.sort(compereBy(‘name’))
Prod sort by name
┌─────────┬─────────────┬───────┐
│ (index) │ name │ price │
├─────────┼─────────────┼───────┤
│ 0 │ ‘Memory’ │ 20 │
│ 1 │ ‘Protector’ │ 9 │
│ 2 │ ‘phone’ │ 699 │
products.sort(compereBy(‘price’))
console.table(products);
Prod sort by price
┌─────────┬─────────────┬───────┐
│ (index) │ name │ price │
├─────────┼─────────────┼───────┤
│ 0 │ ‘Protector’ │ 9 │
│ 1 │ ‘Memory’ │ 20 │
│ 2 │ ‘phone’ │ 699 │
└─────────┴─────────────┴───────┘
/What is Anonymous func?
//Anonymous func is func without name
// - display message
let show = function () {
console.log(‘Anonymous func’);
}
show() //Anonymous func
Anonymous functions as arguments. What is result?
setTimeout(() => {
console.log(‘Execute later after 1’);
},1000)
log - Execute later after 1 - after one sec
What is IIFE Immediately invoked function? What is result?
(function() {
console.log(‘IIFE’);
})();
let person ={
name: ‘Lia’,
age: 23
};
(function () {
console.log(person.name +’ ‘ + ‘with age ‘ + person.age);
})(person)
//IIFE Immediately invoked function execution create a function and execute it immediately after the declaration
// IIFE
//Lia with age 23
What is Arrow functions ? What is result?
let show3 = () => console.log(‘Arrow func’);
show3()
let add3 = (a,b) => console.log(a+b);
add3(5,6)
Arrow functions expressions that provide a shorthand for declaring anonymous functions
show3() //Arrow func
add3(5,6) //11
What are JavaScript Callbacks?
let num = [1, 2, 3, 11, 22, 33, 44];
const isOdd = (n) => n % 2;
console.log(num.filter(isOdd));
Callback is a function that you pass into another function as an argument for executing later.