Funtion Flashcards

1
Q

WHAT TYPE OF FUNCTION?

function isEven(num) {  
  return num % 2 === 0;
}
isEven(24); // => true  
isEven(11); // => false
A

FUNCTION declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the 6 types of functions?

A
Function declaration
Function expression
Shorthand method definition
Arrow function
Generator function
Function constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
console.log(typeof hello);    // => 
function hello(name) {  
  return `Hello ${name}!`;
}
A

‘function’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
console.log(hello.name)       // => 
function hello(name) {  
  return `Hello ${name}!`;
}
A

‘hello’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
function hello(name) {  
  return `Hello ${name}!`;
}
console.log(hello('Aliens')); // =>
A

‘Hello Aliens!’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WHAT TYPE OF FUNCTION?

function sum(a, b) {  
  return a + b;
}
sum(5, 6);           // => 11  
([3, 7]).reduce(sum) // => 10
A

A regular function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

_______means that you declare the function once and later invoke it in many different places.

A

Regular function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

the _________ in a statement always starts with the keyword function.

A

function declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

WHAT TYPE OF FUNCTION?

var isTruthy = function(value) {  
  return !!value;
};
A

function expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
(function() {
  'use strict';
  if (true) {
    function ok() {
      return 'true ok';
    }
  } else {
    function ok() {
      return 'false ok';
    }
  }
  console.log(typeof ok === 'undefined'); // =>
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
(function() {
  'use strict';
  if (true) {
    function ok() {
      return 'true ok';
    }
  } else {
    function ok() {
      return 'false ok';
    }
  }

console.log(ok()); //
})();

A

Throws “ReferenceError: ok is not defined”

because the function declaration is inside a conditional block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
(function() {
  'use strict';
  var ok;
  if (true) {
    ok = function() {
      return 'true ok';
    };
  } else {
    ok = function() {
      return 'false ok';
    };
  }
  console.log(typeof ok === 'function'); // =>

})();

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

HOW TO FIX THIS?

(function() {
  'use strict';
  if (true) {
    function ok() {
      return 'true ok';
    }
  } else {
    function ok() {
      return 'false ok';
    }
  }

console.log(ok()); // Throws “ReferenceError: ok is not defined”
})();

A

make “function ok” into a function expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The 3 things a function expression creates a function object that can be used in different situations:

A
  1. Assigned to a variable as an object count = function(…) {…}
  2. Create a method on an object sum: function() {…}
  3. Use the function as a callback .reduce(function(…) {…})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
var getType = function(variable) {  
  return typeof variable;
};
getType.name // =>
A

empty string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
var getType = function funName(variable) {  
  console.log(typeof funName === 'function'); // => true
  return typeof variable;
}

console.log(getType(3)); // =>

A

‘number’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
var getType = function funName(variable) {  
  console.log(typeof funName === 'function'); // => true
  return typeof variable;
}

console.log(getType.name); // =>

A

‘funName’

18
Q
var getType = function funName(variable) {  
  console.log(typeof funName === 'function'); // => true
  return typeof variable;

console.log(typeof funName === ‘function’); // =>

A

false

19
Q

____________ definition can be used in a method declaration on object literals and ES6 classes. You can define them using a function name, followed by a list of parameters in a pair of parenthesis (para1, …, paramN) and a pair of curly braces { … } that delimits the body statements.

A

Shorthand method

20
Q

3 properties of arrow functions

A
  1. The arrow function does not create its own execution context, but takes it lexically
  2. The arrow function is anonymous: name is an empty string
  3. arguments object is not available in the arrow function
21
Q

RETURNS AND WHY?

class Numbers {  
  constructor(array) {
    this.array = array;
  }
  addNumber(number) {
    if (number !== undefined) {
       this.array.push(number);
    } 
    return (number) function () { 
      this.array.push(number);
    };
  }
}
var numbersObject = new Numbers([]);  
numbersObject.addNumber(0);  
console.log(numbersObject.array)
A

error

function is NOT a arrow function

22
Q

Without the arrow function is necessary to manually fix the context. It means adding fixes by using ______ method:

A

.bind()

23
Q

ECMAScript 6 improves this usage by introducing the __________, which takes the context lexically. This is nice, because from now on is not necessary to use .bind(this) or store the context var self = this when a function needs the enclosing context.

A

arrow function

24
Q

The __________ in JavaScript returns a_______ object. Its syntax is similar to function expression, function declaration or method declaration, just that it requires a star character *.

A

generator function

Generator

25
Q

WHAT TYPE OF FUNCTION?

function* indexGenerator(){  
  var index = 0;
  while(true) {
    yield index++;
  }
}
A

Generator

26
Q

x=>x*2

write as a regular function declaration

A
function (x) {
return x * 2;
}
27
Q

what scope is “VAR”?

A

function scope

28
Q

what scope is “LET”

A

block scope

29
Q
function sum1(a, b) {  
  return a + b;
}
var sum2 = function(a, b) {  
  return a + b;
}
var sum3 = (a, b) => a + b;  
console.log(typeof sum1 === 'function'); // =>
A

true

30
Q
function sum1(a, b) {  
  return a + b;
}
var sum2 = function(a, b) {  
  return a + b;
}
var sum3 = (a, b) => a + b;  
console.log(typeof sum3 === 'function'); // =>
A

true

31
Q
function setupWhatever (){
  console.log(x)
  var x = 3;
  console.log(x);
}

setupWhatever();

//returns?

A

undefined

3

32
Q
function setupWhatever (){
  console.log(x)
  let x = 3;
  console.log(x);
}

setupWhatever();

//returns?

A

syntax error.

let cannot be defined after

33
Q

function setupWhatever (){

  let x;
  console.log(x);
  x = 3;
  console.log(x)
}

setupWhatever();

A

undefined

3

34
Q

what is block scope

A

anything in between curly brackets, if, for….

35
Q

function setupWhatever (){

for (var x = 1; x < 100; x++){

}
console.log(x);
}

setupWhatever();

A

100

36
Q

function setupWhatever (){

for (let x = 1; x < 100; x++){

}
console.log(x);
}

setupWhatever();

A

error.

37
Q

One possible application of _______ is a better way to access the global object in a browser or NodeJS script:

A

new Function

38
Q

The functions created this way don’t have access to current scope, thus closures cannot be created. They are always created in the global scope.

A

new functions

39
Q

If the function should use this from the enclosing function, the________ function is a good solution. When the callback function has one short statement, the function is a good option too, because it creates short and light code.

A

arrow

40
Q

________ way to declare functions generally should not be used. Mainly because it opens potential security risks, doesn’t allow code auto-complete in editors and lose the engine optimizations

A

new Function

41
Q

with JS a outside variable is or is not available inside a function

A

is