0% found this document useful (0 votes)
10 views4 pages

JavaScript Interview Coding Challenges

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

JavaScript Interview Coding Challenges

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Coding Interview Questions

1. Guess the outputs of the following codes


// Code 1:

function func1(){
setTimeout(()=>{
[Link](x);
[Link](y);
},3000);

var x = 2;
let y = 12;
}
func1();

// Code 2:

function func2(){
for(var i = 0; i < 3; i++){
setTimeout(()=> [Link](i),2000);
}
}
func2();

// Code 3:

(function(){
setTimeout(()=> [Link](1),2000);
[Link](2);
setTimeout(()=> [Link](3),0);
[Link](4);
})();

2. Guess the outputs of the following code:


// Code 1:

let x= {}, y = {name:"Ronny"},z = {name:"John"};


x[y] = {name:"Vivek"};
x[z] = {name:"Akki"};
[Link](x[y]);

// Code 2:

function runFunc(){
[Link]("1" + 1);
[Link]("A" - 1);
[Link](2 + "-2" + "2");
[Link]("Hello" - "World" + 78);
[Link]("Hello"+ "78");
}
runFunc();

// Code 3:

let a = 0;
let b = false;
[Link]((a == b));
[Link]((a === b));

3. Guess the output of the following code:


var x = 23;

(function(){
var x = 43;
(function random(){
x++;
[Link](x);
var x = 21;
})();
})();

4. Guess the outputs of the following code:

**Note - Code 2 and Code 3 require you to modify the code, instead of guessing the
output.
// Code 1

(function(a){
return (function(){
[Link](a);
a = 23;
})()
})(45);

// Code 2

// Each time bigFunc is called, an array of size 700 is being


created,
// Modify the code so that we don't create the same array
again and again

function bigFunc(element){
let newArray = new Array(700).fill('♥');
return newArray[element];
}

[Link](bigFunc(599)); // Array is created


[Link](bigFunc(670)); // Array is created again

// Code 3

// The following code outputs 2 and 2 after waiting for one


second
// Modify the code to output 0 and 1 after one second.

function randomFunc(){
for(var i = 0; i < 2; i++){
setTimeout(()=> [Link](i),1000);
}
}
randomFunc();
5. Write the code given If two strings are anagrams of one another, then
return true.

[Link] the code to find the vowels

7. In JavaScript, how do you turn an Object into an Array []?

8. What is the output of the following code?


const b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (let i = 0; i < 10; i++) {


setTimeout(() => [Link](b[i]), 1000);
}

for (var i = 0; i < 10; i++) {


setTimeout(() => [Link](b[i]), 1000);

Common questions

Powered by AI

IIFE, seen in "Code 3" and "Code 1" of Source 1, creates an isolated scope, allowing encapsulation of variables. Inside the IIFE, variables maintain local scope, preventing them from affecting or being affected by the global scope. This leads to temporal dead zone issues if variables are accessed before declaration, requiring careful order of operations inside IIFEs .

Closures trap variables rather than their values, which in the context of "Code 2" in Source 1 results in the loop index `i` being common across iterations. Hence, it logs the last value of `i` (3) multiple times. To correct such behavior, closures can be utilized by capturing `i` inside another function or using `let` instead of `var` to maintain block scope .

JavaScript performs implicit type conversion in expressions. In "Code 2" from Source 1, "1" + 1 results in "11" due to string concatenation, while "A" - 1 results in NaN because 'A' cannot be converted to a number. This demonstrates automatic type conversions in mixed-type operations, often leading to unexpected results .

In nested functions seen in "Code 3" from Source 1, variables can be shadowed by inner scope declarations, as with `var x = 21`. Variable `x` in the outer scope remains accessible until the inner declaration shadows it, emphasizing scoping rules where the innermost declaration overtakes visible names unless modified before re-declaration .

JavaScript hoisting is a behavior where variable and function declarations are moved to the top of their containing scope during the compile phase. This affects outputs like in "Code 1" from Source 1, where variables declared with `var` are hoisted but their assignments are not, making `x` available in the scope after hoisting but before assignment, leading to undefined values in async functions .

In JavaScript, `Object.keys()`, `Object.values()`, and `Object.entries()` can convert objects to arrays, as noted in Source 1. `Object.keys()` gives an array of keys, useful for iterating over property names. `Object.values()` gives an array of values, suitable when values are needed without keys. `Object.entries()` provides key-value pairs and is optimal for paired data manipulation .

In JavaScript, `==` performs type coercion, determining false equality between `0` and `false`, as seen in Source 1. In contrast, `===` respects both value and type for strict equality, leading to more predictable results without automatic conversion, thus making `0 === false` false due to differing types .

In "Code 2" from Source 1, creating the same array upon each function call is inefficient. By moving the array creation outside the function into a closure or global scope, it ensures the array is created only once and reused, reducing memory usage and enhancing performance .

In JavaScript, object keys are always converted to strings. Hence, when setting `x[y]` and `x[z]` in "Code 1" from Source 1, both `y` and `z` are converted to the string 'object Object', leading the last assignment to overwrite previous ones. Thus `x[y]` results in `{name: 'Akki'}` regardless of the original object key .

Closures can capture the current environment's variables, effectively retaining their values within a function. Modifying "Code 3" in Source 1, enclosing the `setTimeout` in another function allows each iteration to retain its index value, achieving the desired output of 0 and 1 after one second by capturing `i` at its current state .

You might also like