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
How to initialize a class variable as an empty array in JavaScript?

I essentially just want to assign the default value of a class variable to an empty array.

I've tried the following, but did not have any luck:

class Test {
    constructor(myArray = []) {
        this.myArray = myArray;
    }
}

Obviously this doesn't work, as when I try to print Test.myArray, undefined is returned rather than an empty array.

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

0

You are trying to access Prototype, your class is initialized correctly, just do:

class Test {
    constructor(myArray = []) {
        this.myArray = myArray;
    }
}

const t = new Test();

console.log(t.myArray)

new keyword means, that you are creating a new instance of the Test class.

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!