JavaScript Functions and Methods Flashcards

1
Q

What is a function in JavaScript?

A

A function is a power tool that helps with reusing code throughout the program

It helps make code dynamic, code can be written once to handle more than one situation

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

Describe the parts of a function definition.

A

The function keyword to begin creating the function

A name for the function, which is optional

Zero or more parameters within parenthesis () separated by commas ,

The function code block within curly braces {}

Statements within the function code block, separated by semicolons ;

function funName (parameters) {
   functionCodeBlock with statements;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the parts of a function call.

A

You call the function by typing its name followed by zero or more arguments within parentheses followed by a semicolon

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

When comparing them side-by-side, what are the differences between a function call and a function definition?

A

The difference is the existence of code block and the swapping of parameters with arguments

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

What is the difference between a parameter and an argument?

A

A parameter is a variable with an undefined value until it is called with arguments

A parameter is a placeholder for arguments

Arguments are adding value to the parameter

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

Why are function parameters useful?

A

It is our way to provide data to the functions and it allows us to get different results

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

What two effects does a return statement have on the behavior of a function?

A

Caused the function to produce a value that can be used in the program

Prevents any more code in the functions code block from being run

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

Why do we log things to the console?

A

It is a powerful tool for debugging and it makes it easy to inspect variables in the browser

It helps the code communicate with us, we can observe data for ourselves

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

What is a method?

A

A method is a function, which is a property of an object

Instance Methods are built-in tasks performed by an object instance

Static Methods are tasks that are called directly on an object constructor

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

How is a method different from any other function?

A

A method is an object reference to a function

An Object Reference is a link to an object

Methods have more access to data and are related to objects

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

How do you remove the last element from an array?

A

You call the pop method of the array object

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

How do you round a number down to the nearest integer?

A

You call the floor method of the Math object with an argument of a numeric value

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

How do you generate a random number?

A

You call the random method of the Math object either with no arguments or arguments with numeric value

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

How do you delete an element from an array?

A

You call the splice method of the array object

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

How do you append an element to an array?

A

You call the push method of the array object

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

How do you break a string up into an array?

A

You call the split method of the variable object followed by an argument that searches for a pattern and picks where the pattern is provided as the first parameter

17
Q

Do string methods change the original string? How would you check if you weren’t sure?

A

No. String methods do not change strings outside of the function.

You can console.log(variable) with the string value or check MDN Developer Tools

18
Q

Roughly how many string methods are there according to the MDN Web docs?

A

There are a lot of string methods.

Make sure you avoid some string methods as they complicate the code.

19
Q

Is the return value of a function or method useful in every situation?

A

No, they are completely situational.

20
Q

Roughly how many array methods are there according to the MDN Web docs?

A

There are a lot of array methods. Make sure you avoid some array methods as they complicate the code.