Interviews10 answers
what is the output for console.log(0.1 + 0.2); console.log(0.1 + 0.2 == 0.3); in JavaScript?
10 Answers
console.log(0.1 + 0.2 == 0.3); True
Guru BandgarPosted May 22, 2016, 10:14 PM EDT
console.log(0.1 + 0.2)=0.30000000000000004
&
console.log(0.1 + 0.2 == 0.3)=false
Abhijeet GuptaPosted Nov 2, 2015, 8:33 AM EST
console.log(0.1 0.2)=0.300000000004
and console.log(0.1 0.2==0.3)= false
pankaj duttPosted Sep 3, 2015, 3:13 AM EDT
1. 0.10.2
2. false
Srinivas MokaPosted Jul 14, 2015, 1:57 AM EDT
1. 0.30000000004
2. false
Pankaj Kumar ChoudharyPosted Jul 2, 2015, 8:26 AM EDT
Read once more :: An educated answer to this question would simply be: “You can't be sure. it might print out “0.3” and “true”, or it might not. Numbers in JavaScript are all treated with floating point precision, and as such, may not always yield the expected results.”
The example provided above is classic case that demonstrates this issue. Surprisingly, it will print out:
0.30000000000000004
Abhishek SharmaPosted Jul 1, 2015, 1:56 AM EDT
Also 0.1 + 0.2 == 0.3, This is also false, Because here it doesn't indicating typeOf check, Its totally different in floating point numbers.
Abhishek SharmaPosted Jul 1, 2015, 1:56 AM EDT
alert(a == b);
alert(a === b);
"==" refers to equals check, while "===" also checks type.
I think you made it wrong Abrar. What the question is... 0.2 +0.1 === 0.30000000000000004, Why? I know, everything in JS is object.
Abhishek SharmaPosted Jul 1, 2015, 1:54 AM EDT
0.30000000000000004
false
see below exmple.
var a = "01";
var b = 01;
alert(a == b) //object comparing ;
alert(a === b) //object type comparing;
output
true
false
Abrar Ahmad AnsariPosted Jul 1, 2015, 1:45 AM EDT
Amazing question, and answer is also surprising.
An educated answer to this question would simply be: “You can't be sure. it might print out “0.3” and “true”, or it might not. Numbers in JavaScript are all treated with floating point precision, and as such, may not always yield the expected results.”
The example provided above is classic case that demonstrates this issue. Surprisingly, it will print out:
0.30000000000000004
Abhishek SharmaPosted Jun 17, 2015, 2:06 AM EDT




