Skip to content
TargetQA

Programming for Automation

JavaScript Interview Questions & Answers

JavaScript interview questions for automation testers: scope and hoisting, closures, promises, async/await, the event loop, array methods and coding problems.

50 questions18 Beginner20 Intermediate12 Advanced

Interview Questions

Showing 15 of 50 questions

  1. var is function-scoped and can be redeclared, while let and const are block-scoped. const prevents reassignment of the binding but does not make objects immutable.

  2. Hoisting is the behaviour where declarations are moved to the top of their scope during compilation. Function declarations are fully hoisted, var is hoisted as undefined, and let/const are hoisted but unusable until their declaration line.

  3. == compares values after type coercion, while === compares both value and type without coercion. In assertions and test logic always use === to avoid false positives.

  4. JavaScript has seven primitive types (string, number, bigint, boolean, undefined, null, symbol) and one non-primitive type, object, which includes arrays and functions. Primitives are copied by value, objects by reference.

  5. map, filter, find, some, every, reduce and forEach are the most common. They are used to transform locator text lists, validate sorting, and pick test data.

  6. Destructuring unpacks values from arrays or properties from objects into variables. Spread expands an iterable or object, and rest collects remaining items into one variable.

  7. CommonJS uses require and module.exports and loads synchronously, while ES modules use import/export and are statically analysable. Node 20+ supports both; ESM is enabled by "type": "module" or the .mjs extension.

  8. A closure is a function that remembers variables from its outer scope even after that scope has finished executing. In automation it is used for data generators, counters and factory functions.

  9. this is determined by how a function is called: by the object before the dot, by new, or explicitly via call/apply/bind. Arrow functions do not have their own this; they inherit it lexically from the surrounding scope.

  10. A Promise represents the eventual result of an asynchronous operation. It is pending, then settles as either fulfilled with a value or rejected with a reason, and the state never changes afterward.

  11. async functions always return a Promise, and await pauses that function until the Promise settles. Forgetting await in Playwright or WebdriverIO makes the test move on before the action completes, causing flaky or falsely passing tests.

  12. JavaScript runs on a single thread with a call stack; async callbacks are queued and executed by the event loop when the stack is empty. Microtasks (Promise callbacks) run before the next macrotask (setTimeout, I/O).

  13. Use try/catch with await, or .catch on Promises, and rethrow when the test should fail. Custom error classes and finally blocks help with clear messages and cleanup.

  14. package.json declares dependencies and scripts, while package-lock.json pins exact resolved versions. npm ci installs strictly from the lockfile and is the right command for CI pipelines.

  15. Playwright and WebdriverIO use native async/await with Promises. Cypress uses a chained command queue with built-in retries, so you use .then() or aliases instead of await.