20 JavaScript tricky program output questions and answers

Umar Farooque Khan
4 min readNov 16, 2023

--

What will be the output ?

Dive into the nuances of JavaScript with this set of 20 tricky output questions. From implicit type conversion to array operations, these questions challenge your understanding of equality, truthiness, and other fundamental concepts. Sharpen your skills and unravel the intricacies of JavaScript with these thought-provoking scenarios.

Question 1:

console.log(2 + '2' - 1);

Explanation :

This involves implicit type conversion. The addition operation converts the number 2 to a string, resulting in the string concatenation of '2' and '1', which is then subtracted, giving the output '21'.

Question 2:

console.log([] == ![]);

Explanation :

Tests truthiness of an empty array. JavaScript converts the array to a boolean (true) and then negates it with !, resulting in false. Comparing an empty array to false using loose equality (==) yields true.

Question 3:

console.log('5' + 3);

Explanation :

The + operator is used for both addition and concatenation. The string '5' is concatenated with the number 3, resulting in the string '53'.

Question 4:

console.log(3 == '3');

Explanation :

JavaScript performs type coercion during loose equality comparison (==). The number 3 is coerced to a string and then compared, resulting in true.

Question 5:

console.log(1 < 2 < 3);

Explanation :

Demonstrates how JavaScript handles chained comparison operators. The expression is evaluated as (1 < 2) < 3, which simplifies to true < 3, ultimately resulting in true.

Question 6:

console.log(3 < 2 < 1);

Explanation :

Chained comparison operators again. This evaluates as (3 < 2) < 1, which simplifies to false < 1, resulting in true.

Question 7

console.log(typeof NaN);

Explanation :

Outputs the type of NaN, which is 'number'. NaN is considered a numeric data type in JavaScript.

Question 8:

console.log(typeof typeof 1);

Explanation :

Shows the type of the string returned by typeof 1. The result is 'string' because typeof 1 is 'number', and the type of a string is always 'string'.

Question 9:

console.log(1 + '1' - 1);

Explanation :

Involves implicit type conversion. The addition operation converts the number 1 to a string, resulting in the string concatenation of '1' and '1', which is then subtracted, giving the output 10.

Question 10:

console.log([] + [] + 'foo'.split(''));

Explanation :

Illustrates array concatenation and string splitting. The empty arrays are concatenated into an empty string, and then ‘foo’ is split into an array of characters (‘f’, ‘o’, ‘o’). Finally, everything is concatenated into the string ‘foo’.

Question 11:

console.log(1 < 2 < 3);
console.log(3 < 2 < 1);

Explanation :

Demonstrates the importance of understanding how JavaScript handles chained comparisons. The first line outputs true and the second line outputs true as well, which might be surprising at first.

Question 12:

var x = 0;
console.log(x++);
console.log(++x);

Explanation :

Uses post-increment (x++) and pre-increment (++x). The first line outputs the current value of x (0) and then increments it, while the second line increments x first and then outputs the result (2).

Question 13:

console.log('1' - - '1');

Explanation :

Utilizes the unary negation operator - to convert the second '1' to a negative number, resulting in the addition of two positive numbers, giving the output 2.

Question 14:

console.log(!!null);
console.log(!!undefined);

Explanation :

Uses the double negation (!!) to convert values to their boolean equivalents. Both null and undefined are falsy values, so the output for both lines is false.

Question 15:

console.log(false == '0');
console.log(false === '0');

Explanation :

Compares a boolean (false) to a string ('0'). The loose equality (==) performs type coercion, so the first line outputs true. The strict equality (===) checks both value and type, so the second line outputs false.

Question 16:

var a = { b: 1 };
var c = a;
a.b = 2;
console.log(c.b);

Explanation :

Highlights how objects in JavaScript are references. Changing the property b of object a also affects object c because they reference the same object. The output is 2.

Question 17:

console.log('hello' instanceof String);

Explanation :

Demonstrates the instanceof operator, which checks if an object is an instance of a particular class. However, 'hello' is a primitive string, not a String object, so the output is false.

Question 18:

console.log(1 + '1' - 1);

Explanation :

Involves implicit type conversion. The addition operation converts the number 1 to a string, resulting in the string concatenation of '1' and '1', which is then subtracted, giving the output 10.

Question 19:

console.log([] == 0);

Explanation :

Compares an empty array to the number 0. The array is coerced to an empty string, and loose equality (==) performs type coercion, so the output is true.

Question 20:

console.log([] == ![]);
console.log(!![]);

Explanation :

The first line involves the negation operator !, which converts the empty array to true and then negates it, resulting in false. The second line uses double negation (!!) to convert the array to its boolean equivalent, resulting in true.

conclusion:

In conclusion, the compilation of 20 JavaScript tricky program output questions and answers offers a dynamic and comprehensive exploration of the language’s intricacies. Covering a spectrum of topics, from type conversion to array manipulation, these questions provide a valuable platform for developers to refine their problem-solving skills and deepen their understanding of JavaScript. The accompanying detailed answers serve as insightful guides, demystifying complex concepts. Whether preparing for technical interviews, assessments, or simply aiming to elevate one’s JavaScript proficiency, this collection is a rich resource. Embracing the challenges presented herein not only enhances coding capabilities but also fosters a robust foundation for mastering the nuances of JavaScript programming.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

Responses (3)