5. JavaScript Functions
37 Questions
0 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

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 (D)</p> Signup and view all the answers

What naming convention is recommended for functions that create something?

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

What components are essential in a function declaration?

<p>Function keyword, function name, parameters, and curly brackets (B)</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 (B)</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 (D)</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. (C)</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. (D)</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 (D)</p> Signup and view all the answers

What defines the scope chain in JavaScript functions?

<p>The visibility of variables from outer functions (B)</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>. (A)</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' (C)</p> Signup and view all the answers

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

<p>'Bob' (C)</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 (D)</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 (C)</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 (C)</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 (C)</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 (D)</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 (A)</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 (D)</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. (B)</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. (A)</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... (B)</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. (B)</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 (D)</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. (C)</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. (D)</p> Signup and view all the answers

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

<p>Name: Anonymous - Score: 0 (A)</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 (B)</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; (A), let myFunc = (x, y) =&gt; { return x + y; }; (C)</p> Signup and view all the answers

What does the keyword 'const' signify in JavaScript?

<p>The value is constant and cannot be reassigned (C)</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. (A), It can be redeclared in the same scope. (D)</p> Signup and view all the answers

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

<p>Anonymous (B)</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; (C)</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 (D)</p> Signup and view all the answers

Flashcards

Function Declaration

A way to define a function using the "function" keyword followed by a name, parameters, and code block.

Function Expression

A way to define a function as a value, often assigned to a variable.

Local Variable

A variable declared within a function; only accessible within that function.

Scope Chain

The order in which JavaScript looks for variables; starting with the current function and moving outwards.

Signup and view all the flashcards

Function Naming

Giving a function a name that describes its purpose.

Signup and view all the flashcards

Default Arguments

Providing default values for function parameters if no argument is passed.

Signup and view all the flashcards

Arrow Function

A concise way to write function expressions.

Signup and view all the flashcards

Return Statement

Specifies the value a function sends back to the part of the code that called it.

Signup and view all the flashcards

Outer Variables in Functions

Functions can access and possibly modify variables declared outside their scope.

Signup and view all the flashcards

Function Scope

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

Signup and view all the flashcards

Scope Chain

When a variable isn’t found in the current scope, JavaScript searches for it in the outer scope.

Signup and view all the flashcards

Global Variables

Variables declared outside any function are accessible anywhere in the code.

Signup and view all the flashcards

Local Variables

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

Signup and view all the flashcards

Variable Hoisting (Implicit)

Javascript declarations are 'hoisted' (moved to top), but are assigned undefined first. Important to consider this when working with Javascript code from different sources, which may make the declaration unclear or in an unexpected order.

Signup and view all the flashcards

Nested Functions

Functions can be defined inside of other functions.

Signup and view all the flashcards

Modifying Outer Variables

Functions can directly change or update variables that are defined in the scope that is outside the function.

Signup and view all the flashcards

Function myFunction(a, b)

A function that takes two parameters, 'a' and 'b', and potentially does something with them. It could print values to the console or return a calculated result.

Signup and view all the flashcards

Function Naming Conventions

Choosing descriptive names for functions, like 'showMessage,' or 'calcSum,' to make code easier to understand.

Signup and view all the flashcards

console.log(a);

Prints the value of variable 'a' to the console. This is often used for debugging and seeing what values variables hold while code runs.

Signup and view all the flashcards

return a+b

Specifies the function 'myFunction' will return the sum of 'a' and 'b'.

Signup and view all the flashcards

myFunction()

This part of the code calls and executes the function named 'myFunction'.

Signup and view all the flashcards

Function declaration

A function defined using the 'function' keyword.

Signup and view all the flashcards

Function expression

A function assigned to a variable or used as a value.

Signup and view all the flashcards

Callback function

Function passed as an argument to another function.

Signup and view all the flashcards

setTimeout()

Runs a function once after a delay.

Signup and view all the flashcards

setInterval()

Runs a function repeatedly at a specified interval.

Signup and view all the flashcards

Code readability

Writing code that is easy to understand and maintain.

Signup and view all the flashcards

Parameter

A value passed to a function when called.

Signup and view all the flashcards

console.log()

Displays output in the browser's console.

Signup and view all the flashcards

Default Arguments

Providing default values for function parameters if no argument is passed.

Signup and view all the flashcards

Rest Parameters

A function parameter that accepts any number of arguments as an array.

Signup and view all the flashcards

Arrow Function

A shorter way to write function expressions.

Signup and view all the flashcards

var scope

Variables declared with "var" have function scope.

Signup and view all the flashcards

let scope

Variables declared with "let" have block scope.

Signup and view all the flashcards

const

Variables declared with "const" cannot be reassigned.

Signup and view all the flashcards

Block Scope

Defines the portion of code where a variable is accessible.

Signup and view all the flashcards

Global Variables

Variables declared outside any function are accessible everywhere in the code.

Signup and view all the flashcards

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

JavaScript Functions Overview
12 questions

JavaScript Functions Overview

LongLastingPointOfView avatar
LongLastingPointOfView
Overview of Functions in JavaScript
10 questions
Javascript Functions
10 questions

Javascript Functions

DextrousMendelevium avatar
DextrousMendelevium
Use Quizgecko on...
Browser
Browser