Surya-2k4 /
Javascript-Bootcamp
A comprehensive JavaScript learning bootcamp with daily topic coverage and hands-on code examples 📚💻.
Loading repository data…
itsprinceraj / repository
📚💻 A comprehensive collection of 200 JavaScript output-based interview questions with detailed explanations. Perfect for interview preparation and deepening your understanding of JavaScript.
Here’s a list of 100 JavaScript output-based interview questions along with detailed explanations of each, helping you grasp the key concepts easily.
console.log(typeof null);
Output: "object"
Explanation: In JavaScript, null is considered to be an object for legacy reasons. It's a bug in the language but can't be fixed due to backward compatibility.
console.log(1 + "2" + "2");
Output: "122"
Explanation: JavaScript uses type coercion. Here, 1 is a number, '2' is a string. The number 1 is coerced into a string, resulting in concatenation: '1' + '2' + '2' = "122".
console.log("1" - 1);
Output: 0
Explanation: When using the - operator, JavaScript converts the string '1' into a number and performs the subtraction, resulting in 0.
console.log(true + false);
Output: 1
Explanation: In JavaScript, true is coerced to 1 and false to 0. Hence, 1 + 0 = 1.
console.log(3 > 2 > 1);
Output: false
Explanation: 3 > 2 evaluates to true. Then, true > 1 is evaluated as 1 > 1, which is false.
console.log([] + []);
Output: "" (empty string)
Explanation: When you use + with arrays, JavaScript converts them to strings. Since both are empty arrays, you get an empty string.
console.log([] + {});
console.log({} + []);
Output:
"[object Object]"0Explanation:
[] + {}: Array gets converted to an empty string, and object gets converted to '[object Object]', so the result is "[object Object]".{} is interpreted as a block, and +[] results in 0.console.log(1 + +"2");
Output: 3
Explanation: The unary + operator before '2' converts it to a number, so it's equivalent to 1 + 2 = 3.
let a = [1, 2, 3];
a[10] = 99;
console.log(a.length);
Output: 11
Explanation: Setting a value at index 10 means the array’s length becomes 11, even though there are empty slots between index 3 and 9.
console.log(null == undefined);
Output: true
Explanation: null and undefined are loosely equal but not strictly equal (===).
console.log(!!null);
console.log(!!"");
console.log(!!1);
Output:
falsefalsetrueExplanation: The !! converts a value to its boolean equivalent. null and "" are falsy, while 1 is truthy.
console.log(typeof NaN);
Output: "number"
Explanation: NaN stands for "Not-a-Number", but it is still of type "number" in JavaScript.
let a = 1;
let b = 2;
console.log((a = b));
Output: 2
Explanation: The expression a = b assigns the value of b to a, and the result of the assignment is the value assigned, which is 2.
console.log([] == []);
console.log({} == {});
Output:
falsefalseExplanation: Arrays and objects are compared by reference, not by value. Two different arrays or objects are never equal, even if they have the same contents.
let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a);
Output: [1, 2, 3, 4]
Explanation: b is a reference to the same array as a, so pushing to b also affects a.
console.log([] == ![]);
Output: true
Explanation: The ![] converts the empty array to false. Then, [] == false becomes true because of type coercion ([] is falsy).
console.log(typeof function () {});
Output: "function"
Explanation: Functions in JavaScript are of the type "function".
let a = [1, 2, 3];
delete a[1];
console.log(a);
console.log(a.length);
Output:
[1, empty, 3]3Explanation: delete removes the value but does not update the length of the array. It leaves an empty slot (undefined).
let a = 5;
console.log(a++);
console.log(++a);
Output:
57Explanation:
a++ returns the current value (5), then increments a to 6.++a increments a first (7), then returns it.console.log(typeof typeof 1);
Output: "string"
Explanation:
typeof 1 returns "number".typeof "number" returns "string" because "number" is a string.console.log("5" - 3);
Output: 2
Explanation: The - operator forces the string "5" to be converted into a number, so 5 - 3 = 2.
console.log([1, 2] + [3, 4]);
Output: "1,23,4"
Explanation: When arrays are concatenated using +, they are first converted to strings. ['1,2'] + ['3,4'] = "1,23,4".
console.log(0.1 + 0.2 == 0.3);
Output: false
Explanation: Due to floating-point precision errors, 0.1 + 0.2 equals something like 0.30000000000000004, not exactly 0.3.
let a = 0;
console.log(a++);
console.log(a);
Output:
01Explanation: a++ increments the value of a but returns the previous value.
console.log([] == ![]);
Output: true
Explanation: ![] evaluates to false, and [] == false is coerced to true because of JavaScript's type coercion rules.
console.log({} + {});
Output: [object Object][object Object]
Explanation: When objects are concatenated, they are converted to strings, resulting in "[object Object][object Object]".
console.log(3 + "3" - "3");
Output: 30
Explanation: '3' - '3' is 0, and 3 + '0' results in 30.
console.log(true == "1");
Output: true
Explanation: The string '1' is coerced
into the number 1, and true is also coerced into 1, making them equal.
console.log(!!false == !!"");
Output: true
Explanation: Both false and '' are falsy values. Using the !! operator converts them into false, so the result is true.
console.log([] == ![]);
Output: true
Explanation: The ![] is evaluated as false, and [] == false results in true due to type coercion.
console.log([] == false);
Output: false
Explanation: [] is an empty array and false is a boolean. When comparing different types, JavaScript attempts to coerce them to a common type. Here, [] is coerced to a string "" and false to "false", so they are not equal.
console.log([] == 0);
Output: false
Explanation: An empty array [] is coerced to an empty string "" and then to 0 in the context of numerical comparison. However, [] coerces to false in other contexts, but not 0. Thus, [] == 0 is false.
console.log(0 == "0");
console.log(0 === "0");
Output:
truefalseExplanation:
== performs type coercion, so 0 is coerced to '0', making them equal.=== checks for strict equality without type coercion, so 0 (number) and '0' (string) are not equal.console.log([1, 2] == [1, 2]);
Output: false
Explanation: In JavaScript, arrays are compared by reference, not by value. Even though the arrays have the same contents, they are different objects in memory, so they are not equal.
console.log(1 + "2" - 1);
Output: 11
Explanation: 1 + '2' results in '12' due to string concatenation. Subtracting 1 from '12' coerces '12' back to a number, resulting in 11.
console.log("5" - 3);
console.log("5" * 2);
console.log("5" / 2);
Output:
2102.5Explanation:
'5' - 3 converts '5' to a number, resulting in 2.'5' * 2 converts '5' to a number and multiplies, resulting in 10.'5' / 2 converts '5' to a number and divides, resulting in 2.5.console.log(0.1 + 0.2 == 0.3);
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);
Output:
falsetrueExplanation:
0.1 + 0.2 is not exactly 0.3 due to floating-point precision errors.Number.EPSILON is a tiny number representing the smallest interval between two representable numbers, used to check if the result is within acceptable precision.console.log(typeof null == typeof undefined);
Output: true
Explanation: Both null and undefined have the same type when using typeof, which is "object" for null (legacy bug) and "undefined" for undefined. The comparison is true for the types used in typeof.
console.log([1] == [1]);
console.log([1] === [1]);
Output:
falsefalseExplanation:
== and === compare references, not values. Different array instances even with the same contents are not equal.console.log("a" + +"b");
Output: "aNaN"
Explanation: + 'b' attempts to coerce 'b' to a number, resulting in NaN. Concatenating 'a' with NaN results in 'aNaN'.
console.log("true" == true);
console.log("true" === true);
Output:
falsefalseExplanation:
== performs type coercion, but 'true' is a string and true is a boolean, so they are not equal.=== checks for strict equality without type coercion.console.log(1 + "2" + 2);
Output: "122"
Explanation: 1 + '2' results in '12', and '12' + 2 results in '122' because 2 is coerced to a string.
console.log([] + {});
console.log({} + []);
Output:
"[object Object]"0Explanation:
[] + {}: Array [] is converted to an empty string "", and {} is converted to [object Object].{} is interpreted as a block statement, so {} + [] is equivalent to 0 + [] which results in 0.console.log(1 + "1" - 1);
Output: 10
Explanation: 1 + '1' results in '11'. Subtracting 1 from '11' coerces it back to a number, resulting in 10.
console.log([1] + [2]);
console.log([1] + 2);
console.log(2 + [1]);
Output:
"12""12""21"Explanation:
+, so [1] + [2] becomes "1" + "2".[1] + 2 is "1" + "2".Selected from shared topics, language and repository description—not editorial ratings.
Surya-2k4 /
A comprehensive JavaScript learning bootcamp with daily topic coverage and hands-on code examples 📚💻.
Amidu99 /
This project is a comprehensive Book Store application built using the MERN stack, leveraging the power of MongoDB, Express.js, React, & Node.js. 📚💻
abhisek2004 /
A comprehensive guide to mastering Java, DSA, and Full Stack Web Development. 📚💻 Includes practice problems, projects, and resources to ace interviews and build amazing web apps! 🚀🔥
DawnBee /
🚀 Kickstart your coding journey with this comprehensive Data Structures and Algorithms template. Organized and well-documented, it includes essential algorithms and data structures written in PY. Boost your problem-solving skills and accelerate your coding projects! 📚💻 #DataStructures #Algorithms #CodingTemplates
Web-By-Xhicko /
ALX Higher Level Programming: Learn Python, SQL, JavaScript, HTML, JSON, and CSS with comprehensive lessons. Elevate your programming skills! 📚💻 #ALXProgramming
adishwar-u /
📚💻. Welcome to the Data Structures and Algorithms repository! This repository is a comprehensive collection of implementations and problem-solving techniques for various data structures and algorithms, designed to help programmers.