TESTEROPS

A pragmatic approach to QA and OPS

JavaScript Interview Questions – Part -2

In this series of questions I’ll focus on the questions that have been asked to me as a part of technical interviews for JS SDET. A lot of companies are now switching to JS based test libraries like Playwright, Cypress , WDIO etc. So it is always good to have certain JS knowledge. All of these questions have been recently asked to me in interviews – they are mostly of the type where they will check for outputs or certain concepts.

Below is a code snippet. Can you tell me what will be the error that Typescript will throw to you in this and how I can fix it?

Ans : The error will be thrown at this line

const basemethod : BaseMethod;

and it will be

A class member cannot have the 'const' keyword.

To fix this error we can use

private readonly basemethods: BaseMethods;

In my package.json file, I have this statement

"type" : "module",

what does this signifies?

Ans : If you have this statement in your package.json file, then it means that your project is compatible with ECMAScript modules (ES modules) syntax for importing and exporting code.

When you set "type": "module" in your package.json, it enables ES module behavior in your project. This means that you can use the import and export statements to work with modules:

What is the output of this code

Ans: The output of the first console.log statement is true because == only checks for the equality for the values.

But === checks both for equality and type and hence second output will return FALSE since second statement is creating a new NUMBER object.

What is the use of the fat arrow syntax (=>) in JS

Ans : The fat arrow (=>) syntax in JS is a more concise way of defining functions , instead of using the function keyword. This provides a shorter syntax and capture the surrounding context (the value of this) automatically, which can be particularly useful in certain scenarios.

A simple example

const add = (a, b) => {
    return a + b;
};
console.log(add(3, 5)); // Output: 8

What is the output of this code

Ans : The output of the console.log(arr2) statement would be [1,2,3,4,5]. This signifies the use of the ... or the spread operator in JS.

In the second statement, where the ... operator is being used, a new array arr2 is being created. The spread operator takes the element of arr1 and places them directly in arr2. So the output in the console.log statement will contain elements from both arrays.

What is the output of this code

Ans: This is an example of how we can use Map in JS. We’re using the map to set the key-value pairs in the Map named userMap

The output from console.log(userMap.get(1)); would be Alice, since the key 1 is used for Alice

The out put from console.log(userMap.size); would be 2, since there are two elements.

Read more about JS maps here

What is the output of this code

Ans : If you understand closures in JS, then you’d know that the output of this would be 10. Why?

So we have an outer function, which has a variable x , which has been assigned a value 10.

Inside outer we have a inner function, which has access to the value of x due to the closure property.

Now the outer function returns inner, which in turn prints the value of x.

In the line const closureFunc = outer();, the outer function is invoked, which returns inner which has an access to the value of variable x. So the final output would be 10.

Why would this code throw an error

Ans : Let me explain this line by line.

In the first line, you’re defining value with type any , which is actually a string. Now in the second line, the value is being used a type assertion , where we want to treat the value of value as number. But number doesn’t have a length property.

So the console.log() statement will throw an error

TypeError: Cannot read property 'length' of undefined

Tell me why would this code throw an error

Ans:

This will return in a compilation error. Why? Because in the the method for subtract you have defined that the function expects three values , while when you’re calling it, you’re only providing two. So it will throw an error

8
Compilation Error: Argument of type '(a: number, b: number, c: number) => number' is not assignable to parameter of type 'Operation'.

Explain the output of this code

Ans :

The first array numbers contains an array of numbers. In the second line, we’re creating a new array evenNumbers, which contains only those numbers which satisfy the criteria of num % 2 ===0. This will filter out the odd numbers. The evenNumbers arrray will have [2,4] as output

In the third line, we’re creating a new array again, which is named doubledNumbers, which uses the map method to apply num * 2 conditions on the evenNumbers array.

So final output would be [4,8]

Output of this code

Ans:

Very simple explanation for this – in the method definition for greet you have defined that the method expects a string parameter.

But when the method is called, you’ve not provided any argument value. So it will print

Error: Expected 1 arguments, but got 0.

This block will throw error . Why?

Ans: The function example() tries to print a variable y . However when the console.log() statement is encountered, till that time y is not declared or defined, so it will complain that y is undefined. So the error would be

ReferenceError: y is not defined

There are two files moduleA.ts and moduleB.ts, which are given below

Will this result in an error?

Ans: If you see the code above, in moduleA.ts, you’re importing moduleB.ts, and in moduleB.ts, there is an import of moduleA.ts.

This will result in a circular dependency, which will be highlighted by TS. So it will complain that

Error: Circular dependency detected: moduleA -> moduleB -> moduleA

Explain the output of this code

Ans: In the method getValue(), the code expects a boolean input. On the basis of the input, there is a conditional statement. If the boolean value is true than the function will return a string Hello. Else it will return a number 42.

So in the case of the method where the getValue() is called with a boolean true, the statement in if block will be executed. So the value of result variable will be hello.

At last the console.log() statement will print the length of the variable in result, which is 5

Now the interviewer will ask what if I send const result = getValue(false);

In that scenario, there will be an error , since the conditional statement will result in value of 42 and trying to find length of number will result in an error

TypeError: Cannot read property 'length' of 42

Can you explain with an example – Unhandled Promise Rejection Error

Ans: An Unhandled Promise Rejection Error generally occurs when a promise is rejected and there is no explicit catch or then block to handle that rejection. An Unhandled Promise Rejection Error can create debugging issues in your async code, and may lead to unpredictable behaviour of your system.

Example code :

const rejectedPromise = Promise.reject(new Error("Promise rejection"));

Explain what is the issue here

Ans: The issue here is that forEach method in JS is not promise-aware. So this will cause issue when trying to use async/await syntax here. If you use forEach will async/await, it will not wait for the promises to resolve. And hence all the callbacks will be executed simultaneously without waiting for the promises. This will result in an unpredictable or incorrect behavior when using in an automation suite.

You can fix it by either using a traditional for loop or a for..of loop.