Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

59
Vistas
Javascript - arrays
  • I'm a total beginner in JS. Now trying to handle arrays

  • question: why the code below doesn't work. The aim is to make a function that adding the variable to the arrays but the outcome is still undefined. I hot stucked.

    var b = [];
    var n = 1;
    
    function name() {
      b.push(n);
      console.log(b);
    }
    
    console.log(b);
    
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to call the name() function, then your code will work as expected

var b = []; var n = 1;

function name() { b.push(n); console.log(b); };

name();
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could also create an IIFE (Immediately-invoked function expression, like so:

(() => {
  var b = [], n = 1;
  b.push(n);
  console.log(b);;
})()
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the code you written function name is declared but never used. Try to call the function like this name()

var b = []; var n = 1;
function name() { 
  b.push(n); console.log(b); 
}
name();

chrome developer tools example

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are two ways to do this.

Either make that function IIFE(Immediately Invoked Function Expression) or call that function

IIFEes are generally used for reducing the pollution of global scope.

First way by calling the function explicitly:-

   

var b = [];
var n = 1;

function name() {
  b.push(n);
  console.log(b);
}
name()
console.log(b);

Second way is using IIFE

var b = [];
var n = 1;

(function name() {
  b.push(n);
  console.log(b);
})()
console.log(b);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda