Skip to content

PayalSasmal10/Javascript-Output-Based-Questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 

Repository files navigation

JavaScript output-based interview questions

πŸ™Œ Like this project?

If you found this helpful, support the project by clicking the ⭐ button at the top of the repo! Your star motivates us to add more high-quality content 🌟✨

Contribution Guidelines

If you want to contribute, improve, or suggest changes to this repo, then check out the Contributing Guide

  1. What will be the output
console.log(0 || 3);
console.log(1 || 2);
console.log(0 && 1);
console.log(1 && 2);
πŸ” Click to View Answer

🧾 Output:

3
1
0
2

🧠 Explanation:

  • 0 || 3 β†’ returns 3 because 0 is falsy, so it evaluates to the second operand (3).
  • 1 || 2 β†’ returns 1 because 1 is truthy, so it evaluates to the first operand.
  • 0 && 1 β†’ returns 0 because 0 is falsy, so && returns the first falsy operand.
  • 1 && 2 β†’ returns 2 because both are truthy, so && returns the last operand.

  1. What will be the output
let f = "2";
let a = 4;
console.log(+f + a + 1);
πŸ” Click to View Answer

🧾 Output:

7

🧠 Explanation:

  • let f = "2"; β€” f is a string "2".
  • +f β€” the unary plus converts the string "2" to the number 2.
  • +f + a + 1 β†’ 2 + 4 + 1 β†’ 7.

  1. What will be the output
const a = (1, 2, 3);
console.log(a);
πŸ” Click to View Answer

🧾 Output:

3

🧠 Explanation:

  • The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
  • (1, 2, 3) evaluates to 3.
  • So, a is assigned the value 3, and console.log(a); prints 3.

  1. What will be the output
function multiple(x, y) {
  return x * y;
}
let multi = multiple.bind(this, 7);
console.log(multi(6));
πŸ” Click to View Answer

🧾 Output:

42

🧠 Explanation:

bind(this, 7) creates a new function where:

  • this is explicitly set (in this case to the current this)
  • First argument x is pre-filled with 7
  • You need to pass only the second argument y
  • this inside multiple is not used, so its value doesn't matter for the output.

  1. What will be the output
let i = 0;
for (i = 0; i < 3; i++) {
  const log = () => {
    console.log(i);
  };
  setTimeout(log, 100);
}
πŸ” Click to View Answer

🧾 Output:

3
3
3

🧠 Explanation:

  • let i = 0; for (i = 0; i < 3; i++) {...} runs the loop 3 times with i = 0, 1, 2.

  • Inside the loop, a new function log is created in each iteration, capturing the same i from outer scope.

  • The function log is scheduled using setTimeout(..., 100), which executes after the loop has finished.

  • By the time all three timeouts fire (after ~100ms), the value of i has already become 3. So each call to log() prints 3.

If you wanted it to print 0 1 2, you'd need to capture the value of i for each iteration using let inside the loop or IIFE:

for (let i = 0; i < 3; i++) {
  const log = () => {
    console.log(i);
  };
  setTimeout(log, 100);
}
// Output: 0 1 2

  1. What will be the output
console.log({} == {});
console.log({} === {});
πŸ” Click to View Answer

🧾 Output:

false
false

🧠 Explanation:

In JavaScript, objects are compared by reference, not by value.

  • {} creates a new object each time. So, {} == {} β†’ comparing two different object references β†’ false.

  • {} === {} β†’ strict equality also compares reference still false.

  • They're not the same object in memory, even though they have the same structure (i.e., empty).

  1. What will be the output
console.log(typeof isNaN);
console.log(isNaN === isNaN);

console.log(typeof NaN);
console.log(NaN === NaN);
πŸ” Click to View Answer

🧾 Output:

"function"
true
"number"
false

🧠 Explanation:

  • typeof isNaN returns "function" because isNaN is a built-in function.
  • isNaN === isNaN is true because both refer to the same function.
  • typeof NaN returns "number" because NaN is a special numeric value.
  • NaN === NaN is false because NaN is not equal to anything, even itself.

  1. What will be the output
let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a);
console.log(b);
πŸ” Click to View Answer

🧾 Output:

[1, 2, 3, 4]
[1, 2, 3, 4]

🧠 Explanation:

  • Arrays are reference types. b points to the same array as a.
  • Pushing to b also changes a.

  1. What will be the output
console.log(x);
var x = 10;
if (true) {
  console.log(x);
  let y = 5;
}
console.log(y);
πŸ” Click to View Answer

🧾 Output:

undefined
10
ReferenceError: y is not defined

🧠 Explanation:

  • var x is hoisted, so console.log(x) before assignment is undefined.
  • Inside the block, x is 10.
  • let y is block-scoped, so console.log(y) outside the block throws an error.

  1. What will be the output
console.log(typeof null);
console.log(null instanceof Object);
πŸ” Click to View Answer

🧾 Output:

"object"
false

🧠 Explanation:

  • typeof null returns "object" (quirk in JS).
  • null is not an instance of Object.

  1. What will be the output
const obj = {
  name: "Heer",
  greet: function () {
    console.log(this.name);
    console.log(`My name is ${this.name}`);
  },
};

const greet = obj.greet;
greet();

obj.greet();
πŸ” Click to View Answer

🧾 Output:

undefined
My name is undefined
Heer
My name is Heer

🧠 Explanation:

  • When greet is called standalone, this is undefined (or global object in non-strict mode).
  • When called as obj.greet(), this refers to obj.

  1. What will be the output
// Class based output
class Person {
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

const p = new Person("Heer");
p.greet();
πŸ” Click to View Answer

🧾 Output:

Hello, my name is Heer

🧠 Explanation:

  • The class constructor sets the name.
  • greet() prints the name.

  1. What will be the output
<div id="parent">
  <button id="child">Child</button>
</div>
const parentEle = document.getElementById("parent");
const childEle = document.getElementById("child");

parentEle.onclick = function () {
  console.log("Hello from parent");
};

childEle.onclick = function () {
  console.log("Hello from child");
};
πŸ” Click to View Answer

🧾 Output:

Clicking the button logs "Hello from child".
Clicking elsewhere in the parent logs "Hello from parent".

🧠 Explanation:

  • Each element has its own click handler.
  • Clicking the child triggers only the child's handler.

  1. What will be the output
function multiple(x, y) {
  return x * y;
}

let multi = multiple.bind(this, 7);
console.log(multi(6));
πŸ” Click to View Answer

🧾 Output:

42

🧠 Explanation:

  • The bind() method creates a new function with a fixed this value and initial arguments
  • multiple.bind(this, 7) creates a new function where:
    • First parameter x is permanently set to 7
    • Second parameter y will be provided when calling multi()
  • When we call multi(6):
    • x is 7 (bound value)
    • y is 6 (passed argument)
    • Returns 7 * 6 = 42

About

Javascript-Output-Based-Questions

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •