Skip to content

Instantly share code, notes, and snippets.

@teffcode
Created May 15, 2020 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teffcode/3bb2b9bcd55b7a0d623e9fe59ec53737 to your computer and use it in GitHub Desktop.
Save teffcode/3bb2b9bcd55b7a0d623e9fe59ec53737 to your computer and use it in GitHub Desktop.

๐Ÿ‘‹๐Ÿผ Welcome ๐Ÿ‘‹๐Ÿผ

Quiz banner

Instagram | Twitter | LinkedIn


๐Ÿ‘… Choose your language



๐Ÿš€ English version


What is the output of the following code ?

  A. ["๐ŸŒฎ", "๐Ÿฅ—", "๐Ÿ—"]
  B. ReferenceError
  C. ["๐Ÿ‘•", "๐Ÿฅ—", "๐Ÿ—"]

๐Ÿ‘€ Click here to see the correct answer and explanation
Correct Answer Explanation
C. ["๐Ÿ‘•", "๐Ÿฅ—", "๐Ÿ—"] Arrays are object in JavaScript and they are passed and assigned by reference. Changing the first element of the clothes, will also modify food: That is, food and clothes point to the same object in memory ๐Ÿง 

If you want to know more about "by reference vs. by value" ๐Ÿ‘‰๐Ÿผ Explaining Value vs. Reference in Javascript | Medium and All you need to know on by reference vs by value | freeCodeCamp



๐Ÿš€ Spanish version


ยฟ Quรฉ imprime el siguiente cรณdigo ?

  A. ["๐ŸŒฎ", "๐Ÿฅ—", "๐Ÿ—"]
  B. ReferenceError
  C. ["๐Ÿ‘•", "๐Ÿฅ—", "๐Ÿ—"]

๐Ÿ‘€ Haz click aquรญ para ver la respuesta correcta y su explicaciรณn
Respuesta correcta Explicaciรณn
C. ["๐Ÿ‘•", "๐Ÿฅ—", "๐Ÿ—"] Los arrays son objetos en JavaScript y se pasan y asignan por referencia. Si cambiamos el primer elemento de clothes tambiรฉn se modificarรก el array de food, ya que, food y clothes apuntan al mismo objeto en la memoria ๐Ÿง 

Te dejo estos artรญculos por si quieres saber mรกs sobre el paso por valor y paso por referencia ๐Ÿ‘‰๐Ÿผ Explaining Value vs. Reference in Javascript | Medium and All you need to know on by reference vs by value | freeCodeCamp



@JoelNieto90
Copy link

var food = ["๐ŸŒฎ", "๐Ÿฅ—", "๐Ÿ—"]; // Creamos el arreglo
var clothes = food; // Copiamos la referencia del arreglo food

clothes[0] = "๐Ÿฅผ"; // reasignamos en la posicion [0] del array referencia clothes

console.log(food); //ย ["๐Ÿฅผ", "๐Ÿฅ—", "๐Ÿ—"]

@JoelNieto90
Copy link

Pueden consultar el siguiente material: https://es.javascript.info/array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment