JavaScript Function

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of a function in programming?

  • To create objects
  • To style web pages
  • To store data
  • To perform a specific task or calculation (correct)

Functions allow code to be reused throughout your program.

True (A)

A function is a block of code that can be ______ when needed.

invoked

You have a block of code that calculates the total price with tax and you need to use it in several parts of your application. What should you do?

<p>create a function to encapsulate that code so it can be called whenever needed</p> Signup and view all the answers

In JavaScript, a method is best described as

<p>A function that is a property of an object (B)</p> Signup and view all the answers

A method is what an object ______.

<p>does</p> Signup and view all the answers

The Array object in JavaScript contains both properties (like length) and methods (like push and pop).

<p>True (A)</p> Signup and view all the answers

Given a string variable name = "Alice", which built-in method would you use to convert it to uppercase?

<p>use the toUpperCase() method</p> Signup and view all the answers

The global scope is the default scope for all code running in script mode.

<p>True (A)</p> Signup and view all the answers

Which of the following is NOT a type of scope in JavaScript?

<p>Object scope (D)</p> Signup and view all the answers

Block scope is created with a pair of ______ braces.

<p>curly</p> Signup and view all the answers

You declare a variable with let inside a for-loop. What scope does this variable have?

<p>block scope</p> Signup and view all the answers

The call stack is used by the JavaScript interpreter to keep track of function calls.

<p>True (A)</p> Signup and view all the answers

The call stack follows which of the following order?

<p>Last-In, First-Out (LIFO) (B)</p> Signup and view all the answers

When a function completes execution, its execution context is ______ off the call stack.

<p>popped</p> Signup and view all the answers

If function A calls function B, and then function B calls function C, describe the order in which the functions are executed and removed from the call stack.

<p>A is pushed onto the stack first then B then C</p> Signup and view all the answers

The arguments object is available inside arrow functions.

<p>False (B)</p> Signup and view all the answers

Which statement about the arguments object is correct?

<p>It is array-like but not an actual array (B)</p> Signup and view all the answers

The arguments object has a ______ property that indicates the number of arguments passed to the function.

<p>length</p> Signup and view all the answers

You have a function that uses the arguments object, but you want to use array methods like map or reduce. What can you do?

<p>convert the arguments object to a true array</p> Signup and view all the answers

An IIFE is executed immediately after it is defined.

<p>True (A)</p> Signup and view all the answers

In an IIFE, why do we wrap the function in parentheses?

<p>All of the above (D)</p> Signup and view all the answers

IIFE stands for ______.

<p>immediately invoked function expression</p> Signup and view all the answers

If you need to run some initialization code that should not pollute the global namespace, which pattern should you use?

<p>use IIFE</p> Signup and view all the answers

Arrow functions have their own this keyword.

<p>False (B)</p> Signup and view all the answers

What is one advantage of using arrow functions in JavaScript?

<p>All of the above (D)</p> Signup and view all the answers

Arrow functions use the ______ operator to define the function body.

<p>=&gt;</p> Signup and view all the answers

You need to write a short function that returns the sum of two numbers. Which syntax is most appropriate?

<p>use arrow function</p> Signup and view all the answers

Default parameters allow you to set default values for function parameters if no value is provided.

<p>True (A)</p> Signup and view all the answers

What happens when undefined is passed to a parameter with a default value?

<p>The default value is used (B)</p> Signup and view all the answers

If no value is passed for a parameter with a default value, the parameter is set to its ______ value.

<p>default</p> Signup and view all the answers

Write a simple function greet that prints "Hello, [name]!" but defaults to "Guest" if no name is provided.

<p>function greet(name = &quot;Guest&quot;){ console.log(<code>Hello, ${name}!</code>); }</p> Signup and view all the answers

The rest parameter syntax uses three dots (...) to collect all remaining function arguments into an array.

<p>True (A)</p> Signup and view all the answers

Where must the rest parameter be placed in a function’s parameter list?

<p>At the end (C)</p> Signup and view all the answers

Rest parameters allow a function to accept an ______ number of arguments.

<p>indefinite</p> Signup and view all the answers

You need to create a function that calculates the sum of any number of inputs. How would you implement this using rest parameters?

<p>function sum(...numbers){ return numbers.reduce((total, num) =&gt; total + num, 0); }</p> Signup and view all the answers

A recursive function is one that calls itself during execution.

<p>True (A)</p> Signup and view all the answers

Which of the following is essential for a recursive function to work correctly?

<p>Both A and B (D)</p> Signup and view all the answers

The condition that stops a recursive function from calling itself is known as the ______ case.

<p>base</p> Signup and view all the answers

Write a recursive function to compute the factorial of a number. Clearly indicate the base case and the recursive call.

<p>function factorial(n){ if (n === 0){ return 1; } return n * factorial(n - 1); } console.log(factorial(5));</p> Signup and view all the answers

Recursion always leads to a stack overflow.

<p>False (B)</p> Signup and view all the answers

Which of the following scenarios is best suited for a recursive solution?

<p>Both A and B (C)</p> Signup and view all the answers

In JavaScript, functions can be nested inside other functions.

<p>True (A)</p> Signup and view all the answers

Which of the following best describes a closure?

<p>A function along with its lexical environment (A)</p> Signup and view all the answers

A closure allows a function to ______ access to variables from its enclosing scope even after that outer function has finished execution.

<p>retain</p> Signup and view all the answers

You have a function that returns another function and uses a variable from the outer function. What concept does this demonstrate?

<p>closure</p> Signup and view all the answers

Arrow functions do not have their own arguments object.

<p>True (A)</p> Signup and view all the answers

What is a key difference between the arguments object and rest parameters?

<p>Rest parameters are true arrays (A)</p> Signup and view all the answers

With rest parameters, you can directly use array methods like ______, filter, or reduce on the collected arguments.

<p>map</p> Signup and view all the answers

You are refactoring an older function that uses the arguments object. To modernize your code, what should you replace the arguments object with?

<p>replace it with rest parameters</p> Signup and view all the answers

Flashcards

Capital of France (example flashcard)

Paris

More Like This

JavaScript Functions Quiz
5 questions

JavaScript Functions Quiz

EnviableHeliotrope7209 avatar
EnviableHeliotrope7209
Basic JavaScript Functions Quiz
6 questions

Basic JavaScript Functions Quiz

SustainableAntigorite1088 avatar
SustainableAntigorite1088
JavaScript Functions and Arguments Quiz
10 questions
Use Quizgecko on...
Browser
Browser