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 πβ¨
If you want to contribute, improve, or suggest changes to this repo, then check out the Contributing Guide
- 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β returns3because0is falsy, so it evaluates to the second operand (3).1 || 2β returns1because1is truthy, so it evaluates to the first operand.0 && 1β returns0because0is falsy, so&&returns the first falsy operand.1 && 2β returns2because both are truthy, so&&returns the last operand.
- 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";βfis a string"2".+fβ the unary plus converts the string"2"to the number2.+f + a + 1β2 + 4 + 1β7.
- 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 to3.- So,
ais assigned the value3, andconsole.log(a);prints3.
- 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.
- 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- 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).
- 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 isNaNreturns"function"becauseisNaNis a built-in function.isNaN === isNaNistruebecause both refer to the same function.typeof NaNreturns"number"because NaN is a special numeric value.NaN === NaNisfalsebecause NaN is not equal to anything, even itself.
- 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.
bpoints to the same array asa. - Pushing to
balso changesa.
- 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 xis hoisted, soconsole.log(x)before assignment isundefined.- Inside the block,
xis10. let yis block-scoped, soconsole.log(y)outside the block throws an error.
- What will be the output
console.log(typeof null);
console.log(null instanceof Object);π Click to View Answer
π§Ύ Output:
"object"
false
π§ Explanation:
typeof nullreturns"object"(quirk in JS).nullis not an instance ofObject.
- 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
greetis called standalone,thisisundefined(or global object in non-strict mode). - When called as
obj.greet(),thisrefers toobj.
- 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.
- 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.
- 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 fixedthisvalue and initial arguments multiple.bind(this, 7)creates a new function where:- First parameter
xis permanently set to7 - Second parameter
ywill be provided when callingmulti()
- First parameter
- When we call
multi(6):xis7(bound value)yis6(passed argument)- Returns
7 * 6 = 42