In which scenario is a function expression preferred over a function declaration?

  • When you need the function to be hoisted and accessible before its declaration in the code.
  • When you want to declare a function as a named function to improve code readability and debugging.
  • When you need to define a function inside an object or as a callback function in another function.
  • When you want to declare a function as a global function for reuse across multiple scripts.
A function expression is preferred over a function declaration when you need to define a function inside an object, pass it as a callback function, or use it in a specific local scope. Function expressions are often used in scenarios where you want to create functions on the fly and keep them within a limited scope.

To select elements with a specific class name, you should use the __________ method.

  • getElementById()
  • getElementByClass()
  • querySelectorAll()
  • querySelector()
To select elements with a specific class name, you should use the querySelector() method. This method allows you to select elements using CSS-like selectors, including class names. For example, document.querySelector('.classname') selects all elements with the specified class name.

While working on a project, you found out that the API you are fetching data from sends relevant error messages in the body of the response, even when the request fails. How do you extract and use this error message in JavaScript?

  • Check the response.ok property to determine success and use response.statusText for error messages.
  • Use the response.error property to access the error message.
  • Parse the entire response body as a JSON object and access the error message field.
  • Create a custom error handler function to retrieve the error message from the API.
To extract and use error messages from the API response, you should check the response.ok property to determine success or failure. If it's a failure, you can access the error message using response.statusText. The other options are not the standard way to extract error messages from a fetch response.

How do you ensure that dynamically added elements maintain accessibility features, like ARIA roles?

  • Dynamically set ARIA roles using JavaScript after adding the element.
  • Browsers automatically assign appropriate ARIA roles to new elements.
  • Dynamically added elements cannot have ARIA roles.
  • Add ARIA roles directly in the HTML source code.
To ensure that dynamically added elements maintain accessibility features like ARIA roles, you should dynamically set ARIA roles using JavaScript after adding the element. Browsers don't automatically assign ARIA roles, and adding roles directly in the HTML source code isn't dynamic. Option 3 is incorrect.

Unlike traditional functions, arrow functions do not have their own __________.

  • this
  • arguments
  • return
  • parameters
Unlike traditional functions, arrow functions do not have their own this binding. In arrow functions, this retains the value of the enclosing lexical context, which makes it useful in cases where you want to maintain the context of the surrounding code, such as in callbacks or when defining functions within functions.

The concept that allows JavaScript objects to inherit properties and behavior from an object of another class is known as _________.

  • inheritance
  • extension
  • encapsulation
  • polymorphism
The concept that allows JavaScript objects to inherit properties and behavior from an object of another class is known as "inheritance." Inheritance is a fundamental aspect of object-oriented programming and helps in code reusability.

What will happen if the break statement is omitted in a switch case?

  • The program will continue to execute the following case(s) until a break or the end of the switch.
  • It will skip the current case and move on to the default case.
  • It will throw a syntax error.
  • It will automatically add a break statement, preventing fall-through.
If you omit the break statement in a switch case, JavaScript will execute the code for that case and continue to execute the code for the following case(s) until it encounters a break statement or reaches the end of the switch statement. This behavior is known as "fall-through."

The keyword ______ is used to specify a block of code to be executed, if the same block that it is directly proceeding does not get executed.

  • break
  • continue
  • return
  • finally
The keyword finally is used to specify a block of code that will be executed regardless of whether an exception is thrown or not in a try...catch block. It ensures that cleanup code is always executed.

You are working on a web application where you need to fetch data from an API, perform operations on it, and then use it to update the UI. Which JavaScript feature allows you to handle these asynchronous operations more readably and reliably?

  • Callback Functions
  • Promises
  • Event Loop
  • Callback Hell
Promises in JavaScript allow you to handle asynchronous operations more readably and reliably. Promises provide a structured way to work with asynchronous code, making it easier to manage tasks like fetching data from an API, performing operations, and updating the UI when the data is ready.

Which keyword is used to declare a variable with block scope?

  • let
  • var
  • const
  • block
The let keyword is used to declare a variable with block scope in JavaScript. Variables declared with let are limited to the block or scope in which they are defined, which is typically within a pair of curly braces {}.