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.

In JavaScript, a common way to utilize prototypes is by assigning a(n) _________ to an object's prototype property.

  • Array
  • Object Literal
  • Constructor Function
  • Prototype Object
In JavaScript, prototypes are often utilized by assigning a constructor function to an object's prototype property. This constructor function serves as a blueprint for creating objects with shared properties and methods. The prototype object associated with this constructor contains those shared properties and methods.

What is a practical use of closures in JavaScript?

  • Encapsulation
  • Inheritance
  • Code Reusability
  • Memory Management
Closures in JavaScript are often used for encapsulation. They allow variables and functions to be "enclosed" within a function, preventing them from polluting the global scope and providing a level of data privacy. This enables code organization, reduces naming conflicts, and promotes modularity in your programs. Closures are a key feature in achieving encapsulation in JavaScript.

In Internet Explorer, instead of addEventListener, the _________ method is used to attach event listeners.

  • attachEvent()
  • registerListener()
  • listenForEvent()
  • addListener()
In Internet Explorer, the attachEvent() method is used to attach event listeners. This method is specific to Internet Explorer and serves a similar purpose to addEventListener() in other browsers, allowing you to respond to events such as clicks and keypresses.

If you’re using arrow functions to define methods inside a class, those methods will not have access to the class’s instance without using _________.

  • super()
  • this
  • prototype
  • new
If you use arrow functions to define methods within a class, they will not have their own this context. Instead, they inherit the this context from the surrounding scope. To access the class instance within an arrow function, you need to use this. Hence, the correct option is this.

How can you prematurely terminate a while loop?

  • break statement
  • return statement
  • continue statement
  • exit statement
You can prematurely terminate a while loop using the break statement. When a break statement is encountered within the loop, it exits the loop prematurely, regardless of whether the loop's condition is still true. This is useful for stopping the loop when a specific condition is met.

It’s possible to create a switch-like behavior using object literals and the _________ method in JavaScript.

  • forEach()
  • map()
  • reduce()
  • hasOwnProperty()
It’s possible to create a switch-like behavior using object literals and the hasOwnProperty() method in JavaScript. By defining properties in an object literal and then checking if a specific property exists using hasOwnProperty(), you can achieve similar branching behavior as a switch statement.

The property event.target gives access to the _________ that triggered the event.

  • element
  • object
  • source
  • target
The event.target property in JavaScript gives access to the element that triggered the event. It's especially useful in event delegation and can be used to identify which specific element was interacted with in a group of similar elements.