#108 Javascript Interview Questions | JS Output based questions || JS Interview Prep #javascript

preview_player
Показать описание

#udemy #leetcode #geektrust #geeksforgeek #hackerrank #codechef #iit #uptu #aktu
Рекомендации по теме
Комментарии
Автор

For the first console an empty array will be converted to an empty string as you said but in the second console if we go by the same logic it should print false

TechieShree
Автор

In JavaScript, when you compare `false` with an empty array `[ ]` using the `==` operator, the comparison involves type coercion. JavaScript converts both values to a common type before comparing them. Here’s what happens step by step:

1. Type Conversion: JavaScript tries to convert the empty array `[ ]` to a primitive value for comparison. An empty array is first converted to an empty string `" "` when coerced to a primitive.

2. Comparison: The comparison then becomes
`false == " "`.

3. Further Type Conversion: `false` is converted to `0`, and `" "` (an empty string) is also converted to `0`.

4. Final Comparison: `0 == 0` is `true`.

So, `false == [ ]` evaluates to `true`.

The `==` operator performs type coercion, leading to this somewhat non-intuitive result. If you use the `===` operator, which does not perform type coercion, the result would be different:
console.log(false === [ ]); // false

In this case, `false` is a boolean and `[]` is an object, so they are not strictly equal.

And the expression false == ![ ] involves a few steps of type coercion and logical operations. Here's the step-by-step breakdown:

Logical NOT Operation (!): The expression ![ ] is evaluated first. The empty array [ ] is a truthy value, so applying the logical NOT operator (!) to it results in false.

Comparison: The expression then becomes false == false.

Final Comparison: Since both sides are false, the comparison evaluates to true.

pradipmandal
Автор

Good question you are very good question

snjvthakur
Автор

Justification for the second line was not sufficient enough to understand!😢

ji_prajwal_ji
Автор

Sir last mai aapne kaha ki empty array ture hota hai to pahle kese true dikhega jab value ture hai to

vishalksingh
Автор

Bhai ❤ sach mano esa hi question aya the mere exam me aaj bas language python tha me doubt me tha [] is ke empty list k ❤❤❤❤ thank you me isko false hi mana tha pehle true mana tha fir false kar diya ❤❤❤❤❤❤❤❤❤❤

shivdix
Автор

Real usecase of when bug becomes a feature.

demogyani
Автор

why [] logic is differnt in both code . did only NOT operator is changing logic ??

akshaytiwari-vz
Автор

you always provide questions at different level .did like this questions are asked in fresher level interview too ?

akshaytiwari-vz
Автор

what if there was some value present in array ?

kumardivyesh
Автор

Or we can also say for first case that false ==" " so both are falsy values so true

rishabh
Автор

Ye type coercion kisi din mere sir ke baal gayab krakar chordhega 😂😂😂 pta hi nhi lgta kb kaun kis situation me palat jae jaise [] empty array palat gya

ashishkumar-jocj
Автор

2 nd wala ka justification sahi nahi kar paaye aap

jatinsingh-kjrh
Автор

empty array string m kyu convert hoga sir, jabki voto truthy value hai

Prem_mourya
Автор

In JavaScript, the comparison of false with an empty array ([]) can be tricky due to type coercion. Here's what happens in these two statements:
console.log(false == [])
console.log(false != [])

1. console.log(false == [])

Explanation: When using the == operator, JavaScript tries to coerce the two operands to a common type. In this case, false is coerced to 0 and an empty array ([]) is coerced to an empty string (""). An empty string is then coerced to false, so we end up with false == false, which is true.

Output: true

2. console.log(false != [])

Explanation: Here, the != operator also involves type coercion. Just like with ==, false is coerced to 0 and [] is coerced to an empty string (""). The empty string is then coerced to false, leading to false != false, which is false.

Output: false

So, when you run these two lines, the output will be:

console.log(false == []) // true
console.log(false != []) // false

sandeep_anshu_
Автор

Pro tip for javascript:
DONT USE ==, always use ===

xtraszone
Автор

console.log(false == !0)
2nd explanation is wong

shubham
Автор

nahi wo sab theek hai par
pehle wale mein truthy value q nahi hua
aur doosre mein common type q nahi banaya

thecommunist
Автор

Bhai dono alag datatype hai to number me convert joga smj nahi aya 😅😅

Trendi_Vibes
Автор

As a senior dev, companies dont need people who know this type of nonsense, just give some value to the company by making bug free optimised product 😅

allmighty
visit shbcf.ru