Instagram | Twitter | LinkedIn
What is the output of the following code ?
A. true true
B. true false
C. false true
D. false false
馃憖 Click here to see the correct answer and explanation
Correct Answer | Explanation |
---|---|
D. false false | JavaScript has both type鈥揷onverting comparisons and strict. The type鈥揷onverting comparison (== ) does the necessary type conversions before doing the equality comparison (E.g, '1' == 1 // true because both values are converting to the same type and then are compared) and the strict comparison (=== ) does not do the type conversion hence if the two values are not of the same type, when compared, it will return false (E.g, '1' === 1 // false ). In this case, we are comparing objects (carA and carB ) and JavaScript compares internal references which are equal when operands refer to the same object in memory 馃. For this reason, two distinct objects are never equal for either abstract (== ) or strict (=== ) comparisons. |
Explanation based on 馃憠馃徏 Comparison operators | MDN
驴 Qu茅 imprime el siguiente c贸digo ?
A. true true
B. true false
C. false true
D. false false
馃憖 Haz click aqu铆 para ver la respuesta correcta y su explicaci贸n
Respuesta correcta | Explicaci贸n |
---|---|
D. false false | En JavaScript se pueden hacer dos tipos de comparaciones: de conversi贸n de tipo y estricta. La comparaci贸n de conversi贸n de tipo (== ) realiza las conversiones de tipo necesarias antes de hacer la comparaci贸n de igualdad (por ejemplo, '1' == 1 // true porque ambos valores se convierten al mismo tipo y luego se comparan) y la comparaci贸n estricta (=== ) no realiza la conversi贸n de tipo, por lo tanto, si los dos valores no son del mismo tipo, cuando se comparan, se devuelve false (por ejemplo, '1' === 1 // false ). En este caso, estamos comparando objetos (carA y carB ) y JavaScript compara referencias internas que son iguales cuando los operandos se refieren al mismo objeto en la memoria 馃. Por esta raz贸n, dos objetos distintos nunca son iguales para comparaciones abstractas (== ) o estrictas (=== ). |
Explicaci贸n basada en 馃憠馃徏 Operadores de Comparaci贸n | MDN
(function () {
var carA = new Object({ color: "blue" }); // Creamos un objeto con su referencia
var carB = new Object({ color: "blue" }); // Creamos otro objeto con una distinta referencia en memoria al anterior objeto
console.log(carA == carB); // comparaci贸n de conversion de tipo
console.log(carA === carB); // comparaci贸n estricta
})();
Respuesta: D. false false