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

216
Views
Javascript - push String to Array returns Integer

I add some strings in an array.

console.log(arr1); // ['product_1']
let arr2 = arr1.push(name);
console.log(arr2); // 2

Why I receive number 2 in the second log when name is a String too?

  • I tried also let arr2 = arr1.slice().push(name); without success.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

arr.push() modifies the arr itself and returns the length of the resulting array, to do what you want to do, you can do one of the two following methods

const name = "test";
arr1 = ['product_1'];

// Method 1
let arr2 = [...arr1, name]
console.log(arr2);

// Method 2
arr1.push(name);
console.log(arr1);

about 4 years ago · Juan Pablo Isaza Report

0

The number 2 in this case is the length of your array.

Documentation on MDN

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!