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

186
Views
Creating a 'const const' object in javascript

In javascript, if I create a const array, I can still modify the object the variable points to:

// The const declaration creates a read-only reference to a value. 
// It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
const x = [1,2,3];
x.push(4);
console.log(x);
x=55 // But this is illegal and will error
console.log(x);

Is there a way to make the elements in an array immutable as well? Similar to something like const int* const x; in C?

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

0

You can use Object.freeze to prevent an object (or array) from being changed:

const x = [1, 2, 3];
Object.freeze(x);

x.push(4); // This will throw an exception
about 4 years ago · Juan Pablo Isaza Report

0

objects frozen with Object.freeze() are made immutable.

Here are the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

Example:

const x = [1,2,3];
Object.freeze(x);
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!