Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

60
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!