JavaScript Interview Questions

10 JavaScript Problems in Job Interviews – Coding in Telugu

10 JavaScript Problems in Job Interviews – Coding in Telugu

Employers ask these JavaScript coding challenges in interviews to assess a candidate’s problem-solving skills, understanding of language nuances, and ability to think critically under pressure—essential qualities for effective software development roles.

  1. Truthy or Falsy:
console.log([] == false);

JavaScript uses type coercion in comparisons. An empty array [] is truthy, but when compared to a boolean (false), it undergoes type coercion. In this case, both sides are coerced to numbers, resulting in 0 == 0, which is true.

  1. Scope Challenge:
var a = 10;
function foo() {
  console.log(a);
  var a = 5;
}
foo();

This code will output undefined. Although a is declared globally and is accessible inside the function foo, the local variable a inside the function shadows the global variable. The console.log(a) inside foo refers to the local variable before it’s initialized, so it’s undefined at that point.

  1. Type Coercion
console.log(1 + '1'); // "11"

JavaScript performs type coercion when different types are used together. In this case, the number 1 is coerced into a string, and the concatenation results in the string "11".

  1. Closure Confusion
for (var i = 1; i <= 5; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000);
}

Outputs: 6, 6, 6, 6, 6.

This is a common closure mistake. The setTimeout function is asynchronous, and when the callback is executed after the loop has finished, it prints the final value of i, which is 6, for all iterations. To capture the current value of i in each iteration, you can use a block-scoped variable with let instead of var.

  1. Prototypal Inheritance
function Parent() {}
function Child() {}

Child.prototype = new Parent();

var obj = new Child();
console.log(obj.constructor === Child);

ans: False, When you set Child.prototype to an instance of Parent, the constructor property of Child.prototype is overridden. To fix this, you can explicitly set Child.prototype.constructor = Child.

  1. Event Loop Challenge
console.log('Start');

setTimeout(function() {
  console.log('Timeout');
}, 0);

Promise.resolve().then(function() {
  console.log('Promise');
});

console.log('End');
// What will be logged and in what order?

JavaScript’s event loop allows asynchronous operations. The order of execution is Start, End, Promise (due to the Promise.resolve().then), and Timeout (due to setTimeout).

  1. Variable Hoisting
console.log(a);
var a = 5;

Output: undefined

In JavaScript, variable declarations using var are hoisted to the top of their scope during the compilation phase, but the assignments are not. This means that the console.log(a) line is effectively interpreted as:

var a;
console.log(a);
a = 5;

So, a is hoisted to the top, but at the time of the console.log(a), it has not been assigned a value, and uninitialized variables in JavaScript default to undefined.

  1. Equality Check
console.log(null == undefined); 

output: true

The loose equality (==) operator performs type coercion. null and undefined are considered equal after type coercion. if you use (===) then the result will be false. it will to strict type check.

  1. NaN Challenge
console.log(NaN === NaN); 

Output: false

NaN stands for “Not-a-Number” and is a special value in JavaScript. Interestingly, NaN is not equal to itself. This behavior is due to the IEEE 754 floating-point specification, which defines NaN as not equal to any value, including itself. To check if a value is NaN, it’s better to use the isNaN() function.

  1. The ‘this’ Keyword
var obj = {
  value: 42,
  getValue: function() {
    console.log(this.value);
  }
};

var getValue = obj.getValue;
getValue(); // Outputs: undefined

When getValue is called, it’s not called as a method of obj, so this refers to the global object (or undefined in strict mode). To maintain the correct context, you can use obj.getValue.bind(obj) or arrow functions.

10 JavaScript Brain Teasers from Interviews
Crack the Code: 10 Tricky JavaScript Interview Challenges
Interview Insights: Navigating 10 JavaScript Coding Puzzles
Unlocking Secrets: 10 JavaScript Challenges for Interviews

JavaScript Mastery: Tackling 10 Interview Coding Problems
Deciphering Code: 10 Tricky JavaScript Interview Questions
Insider’s Guide: 10 JavaScript Challenges in Interviews

Mastering Interviews: 10 JavaScript Coding Problems Revealed
Challenge Your Skills: 10 JavaScript Interview Coding Puzzles
Crucial Coding: 10 JavaScript Problems in Job Interviews

If you have any questions or doubt's regarding these please do a comment below.

10 JavaScript Problems in Job Interviews – Coding in Telugu

Leave a Reply

Your email address will not be published. Required fields are marked *