js-output
js-output
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);
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?
@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.
@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.