When the interpreter encounters the following code var x = "5"; the typeof x will be _________.

  • "string"
  • "number"
  • "boolean"
  • "undefined"
In JavaScript, the typeof operator is used to determine the data type of a variable. When var x = "5"; is encountered, the value of x is a string because it is enclosed in double quotes. Therefore, typeof x will return "string". It's important to note that JavaScript is dynamically typed, meaning the type of a variable can change during runtime.

How can you add a method to an object in JavaScript?

  • a) By using the Object.addMethod() method.
  • b) By defining a function and assigning it as a property of the object.
  • c) By using the Object.method() function.
  • d) By using the object.method = function() syntax.
You can add a method to an object in JavaScript by defining a function and assigning it as a property of the object. For example, myObject.myMethod = function() { /* method code */ };. While you can use various patterns and techniques for method definition, there's no standard Object.addMethod() or Object.method() function.

How does hoisting behave in function declarations in JavaScript?

  • Function declarations are moved to the top of their containing scope during compilation.
  • Function declarations are not affected by hoisting.
  • Hoisting only applies to variables, not functions.
  • Function declarations are moved to the bottom of the code.
In JavaScript, hoisting is the mechanism by which variable and function declarations are moved to the top of their containing scope during compilation. This means that you can call a function declared with function before it appears in your code, and it will still work. However, it's important to note that only the declarations are hoisted, not the initializations. Understanding hoisting is crucial for writing clean and maintainable JavaScript code.

A _________ object is used to perform HTTP requests in AJAX.

  • XMLHttpRequest
  • JSON
  • DOM
  • Fetch
In AJAX (Asynchronous JavaScript and XML), the XMLHttpRequest object is used to perform HTTP requests asynchronously. It allows you to send and receive data from a server without refreshing the entire web page.

Which property of the event object is commonly used to prevent the default action of the event?

  • event.stopPropagation()
  • event.preventDefault()
  • event.cancelBubble()
  • event.halt()
To prevent the default action of an event in JavaScript, you commonly use the event.preventDefault() method. It stops the default behavior associated with the event, such as preventing a form from submitting or a link from navigating to a new page. This method is crucial for controlling the behavior of events.

You have an object containing user data and need to create an array of strings containing user details in a "key: value" format. Which loop might be most suitable for this task?

  • for...in loop
  • for...of loop
  • while loop
  • forEach() method
The for...of loop is most suitable for iterating over object properties when you want to create an array of strings. It directly iterates over iterable values like arrays and works well for this task by extracting key-value pairs from the object.

Which of the following is NOT a characteristic of closures in JavaScript?

  • Data encapsulation and privacy
  • Persistence of local variables
  • Ability to access outer variables
  • Limited use of memory resources
Closures in JavaScript are powerful because they allow functions to remember and access their outer (enclosing) variables even after the outer function has finished executing. This feature can lead to memory usage if not managed properly, but it's not a limitation or disadvantage of closures per se.

_________ allows a function to access all the variables, as well as other functions, that are in its scope.

  • Closures
  • Callbacks
  • Promises
  • Events
Closures allow a function to access all the variables and other functions that are in its scope when the function was created. This feature enables powerful patterns in JavaScript, like data encapsulation and private variables. Understanding closures is essential for advanced JavaScript development.

Consider a situation where you have a switch statement inside a function, and forgetting to include a break statement leads to a bug. How might this bug manifest in the function’s behavior?

  • The function might return the value associated with the first matching case, and all subsequent code within the switch block will execute as well.
  • The function will throw an error, indicating a missing "break" statement, and won't execute any code within the switch block.
  • The function will automatically insert "break" statements at the end of each case, ensuring correct behavior.
  • The function will ignore the switch statement and continue executing the code outside of the switch block.
If you forget to include a "break" statement in a switch case, it will lead to a bug where the function may not behave as expected. Instead of stopping after the first matching case, the switch statement will "fall through" to subsequent cases, causing unintended behavior. The correct option is to use a "break" statement to exit the switch block after handling a case. JavaScript doesn't automatically insert "break" statements, and it doesn't throw an error for missing "break" statements.

The concept of block scope is introduced in ECMAScript 6 with the new keywords _________ and const.

  • let and var
  • block and const
  • scope and let
  • const and var
Block scope in JavaScript is introduced using the let keyword, which allows variables to be declared with block-level scope, while const is used to declare constants. The var keyword does not provide block scope and was used in older versions of JavaScript.