Functions Flashcards

Review of javascript functions

1
Q

What is the syntax for a function statement/declaration?

A
What is the name for a function written with this syntax?
function calcRectArea(width, height) {
  return width * height;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for a function expression?

A
What is the name for a function written with this syntax?
var getRectArea = function(width, height) {
    return width * height;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a variable’s scope, how does it apply to var, let and const? and what is it?

A

variables declared with var are function scoped and ones defined with let and const are block scoped. a Block is defined by curly braces.

function () {
let example=0; *scope begins*

scope ends}

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

How do we implement default parameters on a function?

What do they do? Why use it?

A
example:
an option during the function definition to set default values to parameters if none for given for them. 
function red(scale1='grey',scale2='white',...rest){}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are rest parameters and how do we implement the use of them on a function?

Why would you use it? and how is it related to the arguments object, also what is that?!

A

What are we implementing below?

function(a,b,…rest){ all will return their respective values with rest being an array of the ‘rest’ of variables.}

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

What is closure and give an example.

Explain closure in a loop with var & let. Why use closure?

A

Closure
Any function that uses a variable from outside the scope involves a closure. Essentially a function/object that is able to preserve private variables even after it’s outer function’s execution context has returned.We use closure to help prevent pollution of the global namespace and it allows us to create private variables.

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

What is recursion and what does it have to do with a function?

Give an example! Why would we use them?

A

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. Most loops can be rewritten in a recursive style

var counter = 10;
while(counter > 0) {
    console.log(counter--);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an execution context, how does that relate to functions and the global context?

A

Global execution context cannot be more than one because only one global environment is possible for JS code execution. Functional execution context (FEC): Functional execution context is defined as the context created by the execution of code inside a function. Each function has it’s own execution context.

So nested functions could be refered to as a stack. Also execution context can be looked at as a map of where we are in the execution of code.

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

Is javascript single threaded?

A

Well, arguably its not true that Javascript is single threaded if you see from the under hood working of browser JS to your JS code, there are thread pools. By single threaded what they mean(browser end) is your JS runs into a single threaded event loop. There is one single thread that handles your event loop.

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

What is an IIFE, and give an example

A
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. 
(function () { statements })();

It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts.

The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.

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

What is this and what kind of interactions happen with functions and this?

Why would we use this?

A

this will make reference to the object it is called in, unless explicitly to do otherwise using call, apply, or bind. Functions do not give a scope reference for this, it will just point to the window object as “this”. in an object named movies, this will refer to the object movies. So by default it points to the window object, implicitly it will point to an object that contains that reference to this, and explicitly it will use the direct reference passed by apply,call or bind! A trick that I like to use is to look at the left side of the function call. The object that is standing before the dot is what the this keyword will be bound to.

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

What are callbacks, what are they used for and how are they implemented?

Why would we use one?

A

Callbacks
Callbacks are a way to make sure certain code doesn’t execute until other code has already finished execution.

function greeting(name) {
  alert('Hello ' + name);
}
function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);

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

What is meant by functions in javascript being first class functions?

A

first class functions functions can be assigned as constants, variables, placed as array elements and even set as values of keys on an object. Additionally, (and most importantly ?!) functions can be returned to and from functions — just like any other data type! (Also refered to as high order functions the returning and taking functioions as arguments)

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

Give an example of a function being returned to a value of a variable, how do we now execute the new code set to that variable?

Give an example, and explain why we would want to do this.

A

const addWrapper = () => (x,y) => x + y;

const add = addWrapper();

const sum1 = add (1,2); // 3

// Or we could do it like this

const sum2 = addWrapper()(4,4); // 8

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

What is a method, how does that apply to functions?

What is the difference between a function and method?

A

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Property.

Difference is that a method is the first execution context attached to an object as a property.. all other nested functions are treated as functions and don’t retain the this keyword unless it is a arrow function.

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

How would one call a method?

A

object.method1();

17
Q

What are arrow functions, what is the syntax and what are the benefits if any?

A

An arrow function expression has a shorter syntax than a function expression and does not have its own this , arguments , super , or new.target . These function expressions are best suited for non-method functions, and they cannot be used as constructors

const test =()=>console.log(‘arrow function’);

Remember arrow functions do not use return and if used as a method of an object the this keyword will point to the global window object!

18
Q

What is a high order function?

A

A higher-order function is a function that does at least one of the following: takes one or more functions as arguments, returns a function as its result.

19
Q

How is hoisting dealt with when it comes to functions? What are the two types of functions.

A

http://adripofjavascript.com/blog/drips/variable-and-function-hoisting.html

20
Q

What is recursion? how do we implement it example?

A
function factorial(x) {
  if (x < 0) return;
  if (x === 0) return 1;
  return x * factorial(x - 1);
}

factorial(3);