DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Updated on

 

Paracetamol.js馃拪| #16: 驴Qu茅 imprime este c贸digo JavaScript?

驴Qu茅 imprime este c贸digo JavaScript?

const obj = { 1: "a", 2: "b", 3: "c" };
const set = new Set([1, 2, 3, 4, 5]);

obj.hasOwnProperty("1");
obj.hasOwnProperty(1);
set.has("1");
set.has(1);
Enter fullscreen mode Exit fullscreen mode
  • A: false true false true
  • B: false true true true
  • C: true true false true
  • D: true true true true

Repeusta en el primer comentario:


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:

  • C: true true false true

En el objeto:

const obj = { 1: "a", 2: "b", 3: "c" };
obj.hasOwnProperty("1"); //true
obj.hasOwnProperty(1); //true
Enter fullscreen mode Exit fullscreen mode

El m茅todo hasOwnProperty propio de los objetos retorna un boolean dependiendo si la key del objeto existe o no.
Lo que hay que tener en cuenta es que las claves de un objeto siempre son de tipo string aunque no lo especifiquemos.

En el set:

const set = new Set([1, 2, 3, 4, 5]);
set.has("1"); //false
set.has(1); //true
Enter fullscreen mode Exit fullscreen mode

Esto no funciona como en un objeto, recuerda que un set es como un tipo de arreglo de valores no repetidos. Por ello 1 string no concuerda con 1 number.

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App鈥檚 Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.