What happens when a function declaration and a var variable are hoisted in the same scope?

  • The var variable takes precedence and shadows the function declaration.
  • The function declaration takes precedence and shadows the var variable.
  • JavaScript throws an error since function declarations and var variables can't be hoisted in the same scope.
  • They both coexist in the same scope, and there's no shadowing or precedence.
When a function declaration and a var variable with the same name are hoisted in the same scope, the function declaration takes precedence and shadows the var variable. This is known as "hoisting," and it means that the function is accessible throughout the scope, even before its actual declaration in the code.

While working with React, you notice a function defined using the function keyword is not updating the component state as it should. You suspect it's related to the "this" keyword. What might be the problem?

  • The function should be an arrow function
  • "this" in a React component refers to the element
  • "this" in a React component refers to the component
  • The function lacks proper binding
In React, when using the function keyword to define a custom method within a component class, you need to manually bind the function to the component instance in the constructor using "this.functionName = this.functionName.bind(this);" to ensure that "this" refers to the component instance and not the function itself.

Unlike function expressions, function declarations are _________.

  • Hoisted
  • Anonymous
  • Scoped
  • Encapsulated
Unlike function expressions, function declarations are hoisted. This means they are moved to the top of their containing scope during compilation, allowing you to call them before they are defined in the code. Function expressions are not hoisted in the same way.

JavaScript was initially designed to make web pages more _________.

  • Interactive
  • Static
  • Secure
  • Colorful
JavaScript was initially designed to make web pages more interactive. It allowed developers to create dynamic and engaging web content by manipulating elements on web pages in response to user actions. This interactivity revolutionized web development.

How can you handle exceptions within a "for" loop to prevent it from being terminated prematurely?

  • By wrapping the entire loop in a try-catch block
  • By using the "continue" statement
  • By adding a "finally" block after the loop
  • By setting a maximum execution time for the loop
To handle exceptions within a "for" loop, you can wrap the entire loop in a try-catch block. This allows you to catch and handle exceptions that occur during the loop's execution, preventing it from being terminated prematurely. The other options do not directly address exception handling within the loop.

How does an arrow function handle the "this" keyword differently than regular functions?

  • Arrow functions inherit the "this" value from their containing scope.
  • Arrow functions have their own "this" context.
  • Arrow functions automatically bind "this" to the global object.
  • Arrow functions can't use "this" keyword.
Arrow functions behave differently from regular functions when it comes to the "this" keyword. They inherit the "this" value from their containing lexical (surrounding) scope, while regular functions have their "this" determined by how they are called. This behavior can be advantageous in certain situations.

The _________ method is used to bind an object context to a function and is called immediately.

  • Bind
  • Apply
  • Call
  • Invoke
The "call" method in JavaScript is used to bind an object's context to a function and is called immediately. It allows you to specify the value of "this" explicitly when invoking a function, along with any additional arguments you want to pass to the function.

How can you select all

elements within a specific parent element?

  • document.getElementsByTagName('p')
  • document.select('p')
  • document.querySelectorAll('p')
  • parentElement.querySelectorAll('p')
To select all

elements within a specific parent element, you can use the querySelectorAll() method on the parent element. This method allows you to specify the CSS selector within the context of the parent element, ensuring that only

elements within that parent are selected. The other options are incorrect for this purpose.

How can you prevent script injection attacks when dynamically modifying element content with user input?

  • Use the innerText property to set the content.
  • Use the innerHTML property to set the content.
  • Use a library like jQuery to sanitize input data.
  • Use textContent property to set the content.
To prevent script injection attacks, it's crucial to use the textContent property to set content dynamically. Unlike innerHTML, which parses and executes scripts, textContent treats input as plain text, reducing the risk of script injection. Using libraries may help but doesn't guarantee security. innerText has limited browser support.

You are developing a function that needs to maintain state between calls without using global variables. Which JavaScript feature would you utilize to achieve this?

  • Closures
  • Prototypes
  • Callbacks
  • Promises
You can utilize closures in JavaScript to maintain state between function calls without relying on global variables. A closure is a function that has access to variables from its outer (enclosing) function's scope, even after the outer function has finished executing. This allows you to create private variables and maintain state within the closure function.