DEV Community

Cristian Fernando
Cristian Fernando

Posted on

Paracetamol.js💊| #92: Explica este código JavaScript

Explica este código JavaScript

const a = [1, 2, 3]; const b = [1, 2, 3]; const c = [1, 2, "3"]; console.log(JSON.stringify(a) === JSON.stringify(b)); //? 🤔 console.log(JSON.stringify(a) === JSON.stringify(c)); //? 🤔 
Enter fullscreen mode Exit fullscreen mode

A. true, false
B. false, false
C. false, true
D. true, true

⬇ Respuesta ⬇

A. true, false

JSON.stringify convierte al los arreglos en cadenas.
Para los arreglos a y b tendríamos:

console.log("[1, 2, 3]" === "[1, 2, 3]"); //true 
Enter fullscreen mode Exit fullscreen mode

Para los arreglos a y c tendríamos:

console.log("[1, 2, 3]" === "[1, 2, "3"]"); //false 
Enter fullscreen mode Exit fullscreen mode

Son simples comparaciones de primitivos, en este caso de cadenas.
Usar JSON.stringify es muy común cuando se quiere verificar si 2 arreglos son iguales o no.


Top comments (0)