Functions Flashcards

To learn about functions in js (30 cards)

1
Q

What is a function in JavaScript?

A

A reusable block of code that performs a specific task.

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

How do you define a function?

A

Use the function keyword. Example: function greet() { console.log('Hello'); }

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

How do you call or invoke a function?

A

Use the function name followed by parentheses. Example: greet();

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

What are function parameters?

A

Variables listed in the function definition used to accept input values.

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

What are function arguments?

A

Actual values passed into a function when it’s called.

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

How do you return a value from a function?

A

Use the return keyword. Example: function add(a,b){ return a+b; }

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

What happens if no return statement is used?

A

The function returns undefined by default.

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

What is a function expression?

A

A function assigned to a variable. Example: const add = function(a,b){ return a+b; };

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

What is an arrow function?

A

A shorter syntax for writing functions. Example: const add = (a,b) => a+b;

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

How do arrow functions differ from regular functions?

A

Arrow functions don’t have their own this, arguments, or prototype.

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

What is a default parameter?

A

A value assigned to a parameter if no argument is provided. Example: function greet(name='Guest'){...}

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

What is the arguments object?

A

An array-like object containing all arguments passed to a function.

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

What is the difference between parameters and arguments?

A

Parameters are placeholders in the definition; arguments are actual values passed during execution.

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

What is a callback function?

A

A function passed as an argument to another function and executed later.

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

What is a higher-order function?

A

A function that takes another function as an argument or returns a function.

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

What is a pure function?

A

A function that always returns the same output for the same input and has no side effects.

17
Q

What is a side effect in a function?

A

Any change outside the function’s scope, like modifying a global variable or DOM element.

18
Q

What is recursion?

A

When a function calls itself until a condition is met.

19
Q

What is function scope?

A

Variables declared inside a function are accessible only within that function.

20
Q

What is lexical scope?

A

A function’s scope is determined by where it’s defined, not where it’s called.

21
Q

What is a closure?

A

A function that retains access to variables from its outer scope even after the outer function has returned.

22
Q

What is an IIFE (Immediately Invoked Function Expression)?

A

A function that runs as soon as it’s defined. Example: (function(){ console.log('Run'); })();

23
Q

What is the this keyword in functions?

A

this refers to the object that owns the function, or the global object if none.

24
Q

How can you bind this to a specific object?

A

Use .bind(), .call(), or .apply(). Example: func.call(obj);

25
What does `.bind()` do?
Returns a new function with `this` set to the provided value.
26
What is function hoisting?
Function declarations are moved to the top of their scope before execution, so they can be called before they are defined.
27
Are function expressions hoisted?
No, function expressions are not hoisted.
28
How do you create an anonymous function?
Define a function without a name. Example: `function(){ console.log('Hi'); }`
29
How do you store functions in objects?
Assign them as properties (methods). Example: `const obj = { greet(){ console.log('Hi'); } };`
30
How do you use a function as a constructor?
Call with `new`. Example: `function Person(name){ this.name = name; } const p = new Person('John');`