90 % Fail To answer theses JavaScript snippets

Vineet Mishra
1 min readAug 8, 2022
Q1. Guess the output

console.log(0.1 + 0.2);

console.log(0.1 + 0.2 == 0.3);

Take a breath, don’t jump directly into solution

Output:
0.30000000000000004
False

Reason: In this snippet , first one is very straight forward, its the addition of 2 decimal numbers values in the next one , we are testing condition that is true or false

Q2. Guess the output

function getValue1() {

return {

bar: “hello” };

}

function getValue2() {

return

{

bar: “hello”

};

}

console.log(“getValue1 returns:”); 
console.log(getValue1());
console.log(“getValue2 returns:”);
console.log(getValue2());

Wait will this snippet run fine? think hard , don’t jump into solution

getValue1 returns:
{ bar: 'hello' }
getValue2 returns:
undefined

Reason: The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form). As a result, when the line containing the return statement (with nothing else on the line) is encountered in getValue2(), a semicolon is automatically inserted immediately after the return statement.

--

--

Vineet Mishra

SDE 2 at Accelya , I write about Mobile/Web Development and Deployment, and Interview Q & A