Functions Flashcards

1
Q

Declare a function that returns Hello and concatenate using +

A
function greet(firstName, lastName){
 return 'Hello' + ' ' + firstName + ' ' + lastName;
}
console.log(greet('John', 'Doe'));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What would look like this function if it didn’t have attributes in the console? and what will it return?

function greet(firstName, lastName){
 return 'Hello' + ' ' + firstName + ' ' + lastName;
}
console.log(greet('John', 'Doe'));
A
function greet(firstName, lastName){
 return 'Hello' + ' ' + firstName + ' ' + lastName;
}
console.log(greet());

WILL RETURN

Hello undefined undefined

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

Study this function and tell what is happening in it.

function greet(firstName = 'John', lastName = 'Doe'){
  return 'Hello ' +  firstName + ' ' + lastName;
 }
 console.log(greet('cc', 'xx'));
A
function greet(firstName = 'John', lastName = 'Doe'){
  return 'Hello' + ' ' + firstName + ' ' + lastName;
 }
 console.log(greet('cc', 'xx'));

In here we are assigning a default value to the parameters firstName and lastName, meaning that if we don’t call anything in our console.log, it will return such a value since it’s default however if we assign anything to our console.log it will write over the default and will show the value assign

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

What is a function expression?

A

It’s basically a variable that has become a function since the function it’s assigned to the variable and it will be executed every time the variable it’s called.

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

show a function expression.

A
const square = function(x){
  return x*x;
};

console.log(square(4));

// The reason why you have to console.log square(5) it’s because square it’s calling the function within it, but you have to assign a value to your parameter that it’s within your function and that value will be assign in the console.log by giving it to square

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

Assign a default value to x

const square = function(x){
  return x*x;
};

console.log(square(4));

A
const square = function(x = 4){
  return x*x;
};

console.log(square());

// In this case x = 4 because what X is looking for it’s a number to be multiplied by, we cannot assign a string to X as default because it will return NaN

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

What is IMMEDIATELY INVOKABLE FUNCTION EXPRESSIONS - IIFEs

A

These are functions that you declare and run at the same time.

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

What does an IMMEDIATELY INVOKABLE FUNCTION EXPRESSIONS - IIFEs look like?

A

(function(){
console.log(‘IIFE Ran..’)
})()

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

How are methods create?

A

Methods are created when a function its put inside of a object it’s called method.

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

Type a method

A
const todo = {
  add: function (){
    console.log('Add todo..');
  },
  edit: function(id){
    console.log(`Edit todo ${id}`);
  }
}

todo.delete = function(){
console.log(‘Delete todo…’)
}

todo. add();
todo. edit(22);
todo. delete();

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