Javascript Functions
10 Questions
1 Views

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

Which of the following statements is MOST accurate regarding the primary benefit of using functions in programming?

  • Functions enhance code readability by automatically adding comments throughout the script.
  • Functions automatically optimize code execution speed by compressing the script size.
  • Functions eliminate the need for variable declarations, simplifying the overall code structure.
  • Functions facilitate code reusability, reducing redundancy and improving maintainability. (correct)

Consider the following JavaScript code:

`function outerFunction() { let outerVar = 'Outer'; function innerFunction() { let innerVar = 'Inner'; alert(outerVar); } innerFunction(); alert(innerVar); }

outerFunction();`

What will be the output of this code?

  • Alerts 'Outer', then alerts 'Inner'.
  • Throws an error because neither `outerVar` nor `innerVar` are globally defined.
  • Alerts 'Inner', then alerts 'Outer'.
  • Alerts 'Outer', then throws an error because `innerVar` is not defined in the `outerFunction` scope. (correct)

What is the correct syntax to define a function named calculateSum that accepts two parameters, a and b, and returns their sum?

  • `calculateSum(a, b) => { return a + b; }`
  • `calculateSum = function(a, b) { return a + b; }`
  • `function calculateSum(a, b) => a + b;`
  • `function calculateSum(a, b) { return a + b; }` (correct)

Given the following JavaScript code:

`let globalVar = 10;

function modifyGlobal() { globalVar = 20; }

function checkValue() { let globalVar = 5; console.log(globalVar); }

modifyGlobal(); checkValue();`

What value will be logged to the console, and why?

<p>5, because <code>checkValue</code> declares a local variable with the same name, shadowing the global variable. (A)</p> Signup and view all the answers

What is the significance of the parameters in a function definition?

<p>Parameters serve as placeholders for values that will be passed into the function when it is called. (C)</p> Signup and view all the answers

Consider the following JavaScript function:

`function example() { let x = 10; if (true) { let x = 20; console.log(x); } console.log(x); }

example();`

What will be printed to the console?

<p>20, 10 (B)</p> Signup and view all the answers

In JavaScript, what is the purpose of the return statement within a function?

<p>To terminate the execution of the function and optionally send a value back to the caller. (D)</p> Signup and view all the answers

Examine the code snippet below:

`function modifyArray(arr) { arr.push(4); arr = [5, 6, 7]; return arr; }

let myArray = [1, 2, 3]; let newArray = modifyArray(myArray);

console.log(myArray);`

What will be the output of console.log(myArray)?

<p>[1, 2, 3, 4] (B)</p> Signup and view all the answers

What is the difference between a function declaration and a function expression in JavaScript?

<p>Function declarations are hoisted, allowing them to be called before they are defined in the code, whereas function expressions are not hoisted. (A)</p> Signup and view all the answers

Consider the following code involving a recursive function:

`function factorial(n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }

console.log(factorial(5));`

What value will be printed to the console?

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

Flashcards

What are Functions?

Reusable blocks of code that perform a specific task.

Function declaration syntax

The function keyword, a name, parentheses (), and curly braces {}.

What does function's call do?

Executes the code within a defined function.

Local Variable

A named container for a value that's only accessible inside the function where it's defined.

Signup and view all the flashcards

What problem do functions solve?

Writing code once and using it multiple times.

Signup and view all the flashcards

What is one advantage of using functions?

Modify the code in one place and changes occur everywhere the function is used.

Signup and view all the flashcards

Examples of built-in functions

alert(message), prompt(message, default), and confirm(question).

Signup and view all the flashcards

Function Declaration Structure

function functionName(parameters) { // Function body }

Signup and view all the flashcards

How do you call a function?

Using the function's name followed by parentheses: functionName();

Signup and view all the flashcards

Scope of Local Variables.

Variables declared inside a function are not accessible outside of it.

Signup and view all the flashcards

Study Notes

  • Functions are the building blocks that allow code to be called without repetition
  • Built-in functions previously seen include alert(message), prompt(message, default) and confirm(question)

Function Declaration

  • Functions can be created using a function declaration
  • The function keyword goes first, followed by the name of the function
  • Next is a list of parameters between parentheses, comma-separated, can be empty
  • Finally is the code of the function, also named the function body, between curly braces
function name(parameter1, parameter2,... parameterN) {
 // body
}
  • New functions are called by their name
  • Calling showMessage() executes the code of the function
  • Functions help to avoid code duplication
  • Changing the message that is shown only requires modification in one place, so if we need to change the way a message is shown, we only need to change the function

Local Variables

  • A variable declared inside a function is only visible inside that function
 function showMessage() {
  let message = "Hello, I'm JavaScript!"; // local variable

  alert( message );
}

showMessage(); // Hello, I'm JavaScript!

alert( message ); // error! The variable message is local to the function

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Functions are the building blocks that allow code to be called without repetition. Functions can be created using a function declaration. Functions help to avoid code duplication.

More Like This

Javascript Functions: Declaration and Syntax
10 questions
Javascript Function
10 questions

Javascript Function

DextrousMendelevium avatar
DextrousMendelevium
JavaScript Functions: Declaration and Calling
10 questions
Use Quizgecko on...
Browser
Browser