DEV Community

Cristian Fernando
Cristian Fernando

Posted on

 

Paracetamol.js馃拪| #36: Explica este c贸digo JavaScript

Explica este c贸digo JavaScript

let num = 10;

const increaseNumber = () => num++;
const increasePassedNumber = number => number++;

const num1 = increaseNumber();
const num2 = increasePassedNumber(num1);

console.log(num1);
console.log(num2);
Enter fullscreen mode Exit fullscreen mode
  • A: 10, 10
  • B: 10, 11
  • C: 11, 11
  • D: 11, 12

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando • Edited

Respuesta:

  • A: 10, 10

La primera funci贸n en llamarse es increaseNumber que solo se encarga de retornar la variable num y luego la incrementa; num no esta en el scope de la funci贸n por eso pasamos a buscar la variable en el scope global. Esta funci贸n regresar谩 10.

num1 se pasa como par谩metro a increasePassedNumber que hace lo mismo que increaseNumber, regresa primero el valor de la variable y luego la incrementa, por ello obtenemos nuevamente como salida el valor 10.

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it鈥檚 surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!