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

126
Views
Javascript beginner - function modifiing global const

I'm currently learning js and stumbled upon this to me weird behavior. I probably missing something fundamental here, but i can't find the answer.

I have a function that modifies an array and returns it. If i print that out the original array gets modified, even though it's a constant.

const arr = [5, 9, 7, 1, 8, ];

function test(arr) {
  const strings = ['qwe', 'asd'];
  arr.splice(1, 0, ...strings);
  return arr
}

console.log(test(arr))
console.log(arr)
console.log(test(arr))
console.log(arr)
console.log(test(arr))
console.log(arr)

Output shows that the original array gets bigger everytime. I was expecting that it is the same output everytime.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A const means you cannot reassign to it like this

const arr = [1, 2, 3];
arr = [5, 4]; // You cannot do this that's const

other than that you can modify the array contents

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!