0% found this document useful (0 votes)
2 views

js-output

The document contains a series of JavaScript questions and answers, addressing common misconceptions about the language's behavior with floating-point arithmetic, type coercion, and closures. It explains why certain expressions yield unexpected results, such as '0.1 + 0.2' not equaling '0.3', and how JavaScript handles different data types during operations. Each question is accompanied by a concise explanation to clarify the underlying concepts.

Uploaded by

Subham Pattanaik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

js-output

The document contains a series of JavaScript questions and answers, addressing common misconceptions about the language's behavior with floating-point arithmetic, type coercion, and closures. It explains why certain expressions yield unexpected results, such as '0.1 + 0.2' not equaling '0.3', and how JavaScript handles different data types during operations. Each question is accompanied by a concise explanation to clarify the underlying concepts.

Uploaded by

Subham Pattanaik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Can You Answer These

Questions?

@DimpleKumari
Forming a network of fantastic coders.
Question 1: What's the Result of
console.log(0.1 + 0.2 === 0.3);
Answer: The result is false.

Explanation:
In JavaScript, numbers with decimals (called floating-
point numbers) don't always add up the way we expect.
Due to how floating-point numbers are represented in
JavaScript, 0.1 + 0.2 doesn't exactly equal 0.3. Instead,
it results in 0.30000000000000004, leading to the
comparison being false. This issue comes from the binary
approximation of decimal numbers in JavaScript.

@DimpleKumari
Forming a network of fantastic coders.
Question 2: What's the Result of
console.log("5" + 3);
console.log("5" - 3);
Answer: "5" + 3 results in "53".
"5" - 3 results in 2.

Explanation:
"5" + 3: When you use the + sign with a string and a
number, JavaScript treats the number like a part of
the string. So instead of adding 5 and 3 as numbers,
it sticks them together as text, resulting in "53".
"5" - 3: The - operator doesn't work with strings, so
JavaScript converts "5" to a number and subtracts 3,
resulting in 2.

@DimpleKumari
Forming a network of fantastic coders.
Question 3: What's the Result of
console.log(typeof null);

Answer: The result is "object".

Explanation:
This is a weird part of JavaScript. The typeof operator
should tell you what kind of value you have.
But when you check typeof null, JavaScript mistakenly
says it's an object, even though null is actually a special
value that means "nothing."
This is a bug that has been around for a long time and
hasn't been changed to keep old code from breaking.

@DimpleKumari
Forming a network of fantastic coders.
Question 4: How Does a Closure Work?

Answer: The output will be:


1
2

@DimpleKumari
Forming a network of fantastic coders.
Question 4: How Does a Closure Work?

Explanation:
A closure happens when a function remembers the
variables around it, even after the outer function has
finished running.
In this example, the inner function still has access to the
count variable inside outerFunction. Each time you call
closure(), it increases count and shows it.

@DimpleKumari
Forming a network of fantastic coders.
Question 5: What's the Result of
console.log(true + false);
console.log([] + {});
Answer: true + false gives 1.
[] + {} gives "[object Object]".

Explanation:
true + false: In JavaScript, true is treated as 1 and false as
0. Adding 1 + 0 gives 1.

[] + {}: When you add an empty array [] to an empty object


{}, JavaScript changes them to strings. The empty array
becomes an empty string "", and the empty object becomes
"[object Object]". So, adding them gives "[object Object]".

@DimpleKumari
Forming a network of fantastic coders.
Question 6: What's the Result of
console.log([] == ![]);
Answer: The result is true.

Explanation:
This is tricky! Here's what happens:
1. ![] means "not an empty array." An empty array is a
"truthy" value, so ![] is false.
2. Now, the expression is [] == false.
3. JavaScript tries to compare [] and false. It changes
[] into an empty string "" and false into 0.
4. Then, "" == 0 is true because JavaScript changes the
empty string to 0 when comparing.

@DimpleKumari
Forming a network of fantastic coders.
Dimple Kumari
Forming a network of fantastic coders.

You might also like