5. JavaScript Functions
37 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What value will be logged after executing myFunction()? Assume let a; let b; are appropriately declared.

  • undefined
  • -100 (correct)
  • 0
  • true
  • What will be logged when console.log(b); is executed after calling myFunction() that assigns b = true;?

  • null
  • reference error
  • undefined
  • true (correct)
  • What does myFunction(5, 10); log to the console?

  • 5
  • 5 and 10 (correct)
  • 10
  • 15
  • What would be the output of console.log(myFunction(5, 10));?

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

    What naming convention is recommended for functions that create something?

    <p>create...</p> Signup and view all the answers

    What components are essential in a function declaration?

    <p>Function keyword, function name, parameters, and curly brackets</p> Signup and view all the answers

    What will the following code return: function square(number) { return number * number; } when called with square(4)?

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

    How is a function expression different from a function declaration?

    <p>A function expression can be anonymous, while a function declaration cannot</p> Signup and view all the answers

    What will happen if you try to call the function subtract before it is defined as a function expression?

    <p>It will return a TypeError.</p> Signup and view all the answers

    Which statement correctly describes local variables in a function?

    <p>Local variables are visible only within the function where they are defined.</p> Signup and view all the answers

    What is displayed by console.log(value) if let value = square(3); where square is defined as function square(num) { let result = num * num; return result; }?

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

    What defines the scope chain in JavaScript functions?

    <p>The visibility of variables from outer functions</p> Signup and view all the answers

    What will occur if you try to return a non-existing parameter from a function?

    <p>It will return <code>undefined</code>.</p> Signup and view all the answers

    What will be the output of alert(message) when called inside showMessage() if userName is 'Tom'?

    <p>'Hello, Tom'</p> Signup and view all the answers

    What is the value of userName after calling showMessage() when userName is initially 'John'?

    <p>'Bob'</p> Signup and view all the answers

    In the context of scope chain, what happens if a variable is not found in the current scope?

    <p>It searches the outer scope</p> Signup and view all the answers

    If a variable a is declared in both global and function scope, which value will console.log(a) display inside the function if a = 15 is assigned?

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

    What is the order of variable resolution when accessing a variable in a nested function?

    <p>Local scope first, then outer scope, then global scope</p> Signup and view all the answers

    What will the first console.log(a) output if a is declared in the global scope and is then redeclared in myFunction() without initializing it?

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

    What is the result of attempting to alert(message) if the variable message is defined locally in showMessage()?

    <p>It will give a ReferenceError</p> Signup and view all the answers

    When a function modifies an outer variable, what does this imply about the variable's access?

    <p>The variable is mutable</p> Signup and view all the answers

    What is the output of the function 'test' when called with parameters 'a' and 'b' as defined?

    <p>Hello John from London</p> Signup and view all the answers

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

    <p>Function expressions can be stored in variables.</p> Signup and view all the answers

    What does the 'setTimeout' method do in JavaScript?

    <p>It sets a timer and executes a function once after a delay.</p> Signup and view all the answers

    What will be printed as the value of 'i' during the execution of 'setInterval' in the second example?

    <p>1, 2, 3, 4...</p> Signup and view all the answers

    Which of the following statements about callback functions is true?

    <p>They are never executed at the same time as the function that calls them.</p> Signup and view all the answers

    What will be the output of the following code: 'console.log(greet(personName, personCity));'?

    <p>Hello John from London</p> Signup and view all the answers

    How does functional programming differ from imperative programming?

    <p>Functional programming treats computation as the evaluation of mathematical functions.</p> Signup and view all the answers

    Which of the following can be a valid argument for 'setTimeout'?

    <p>Both a function and a number.</p> Signup and view all the answers

    What will be the output of the following code: let scoreText1 = getScoreText();?

    <p>Name: Anonymous - Score: 0</p> Signup and view all the answers

    What does the rest parameter syntax (...theArgs) allow a function to do?

    <p>Accept an arbitrary number of arguments as an array</p> Signup and view all the answers

    Which of the following correctly defines an arrow function with two parameters?

    <p>let myFunc = (x, y) =&gt; x + y;</p> Signup and view all the answers

    What does the keyword 'const' signify in JavaScript?

    <p>The value is constant and cannot be reassigned</p> Signup and view all the answers

    Which of the following statements about the var keyword is true?

    <p>It does not create a local scope.</p> Signup and view all the answers

    In the function getScoreText(undefined, 99), what value will be assigned to the name parameter?

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

    Which of the following is NOT a valid way to define an arrow function?

    <p>let myFunc = x, y =&gt; x * y;</p> Signup and view all the answers

    What will be the value of a inside the function myF after executing var a = 10;?

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

    Study Notes

    Functions

    • Functions are blocks of code designed to perform a specific task.
    • Function declaration: Defines a function using the function keyword, followed by the function name, parameters, and code block.
    • Function expression: Creates a function and assigns it to a variable.
    • Local variables: Variables declared inside a function are only accessible within that function.
    • Scope chain: JavaScript engine searches for a variable's value within the current scope. If not found, it searches in outer scopes.
    • Naming a function: Use descriptive names (e.g., calculateSum, getDetails).
    • Default arguments: Assign default values to function parameters if no argument is provided (e.g., function greet(name = "Guest")).
    • Arrow function expression: A concise way to create functions (e.g., const multiply = (x, y) => x * y).
    • Callback functions: Functions passed as arguments to other functions or returned as values.
    • Rest parameters: Accepts an indefinite number of arguments as an array.
    • Hoisting: Moving function and variable declarations to the top of their current scope.

    Local Variables

    • A variable declared within a function is only accessible inside that function's scope.

    Outer Variables

    • A function can access outer variables, but it's not recommended to modify them directly.

    Scope Chain

    • JavaScript searches for a variable's value within the current scope. If not found, it checks other scopes until it finds the variable or reaches the global scope.

    Variables

    • var: Declares a variable with function scope.
    • let: Declares a block-scope local variable, not accessible outside the block it is defined in.
    • const: Declares a constant (read-only) variable with block scope.

    Global vs. Local Scope

    • Global variables are accessible throughout the program but should be avoided when possible. Local variables are accessible only within the block they are declared in.

    Hoisting

    • JavaScript moves variable and function declarations to the top of the current scope. This may lead to unexpected results if the declared variable or function has been referenced earlier.

    First-Class and Higher-Order Functions

    • First-class functions: Functions can be assigned to variables, passed to other functions and returned as values.
    • Higher-order functions: Functions that take functions as arguments, or return functions as values.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    JavaScript Functions (PDF)

    Description

    Dive into the essential concepts of functions in JavaScript. This quiz covers function declarations, expressions, local variables, scope chain, and more. Test your knowledge on naming conventions, default arguments, arrow functions, callback functions, and rest parameters.

    More Like This

    Functions in Programming
    24 questions

    Functions in Programming

    SimplestLapisLazuli avatar
    SimplestLapisLazuli
    Overview of Functions in JavaScript
    10 questions
    Use Quizgecko on...
    Browser
    Browser