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

231
Views
How to initialize to an empty array if my 'students' parameter is not passed

I am attempting to write code For the Bootcamp class (as shown in the code block) My goal is to write the code for the Bootcamp class so that if it the "students" parameter is not passed in, it is by default initialized to be an empty array from within the constructor parameter list itself. i took a stab at it with the function at the bottom of the code.

I am a bit confused regrading the proper syntax. Please See my Code Below

  • Thank you in advance :)
class Bootcamp{
    constructor(name,level,students){
        this.name = name;
        this.level = level;
        this.students = students;
    }
}
function student (students){
    return [];
} 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use a default value if the param is not passed. In this way, your properties will be initialized with default values.


    class Bootcamp{
        constructor(name = '', level = 0, students = []){
            this.name = name;
            this.level = level;
            this.students = students;
        }
    }

about 4 years ago · Juan Pablo Isaza Report

0

Say you have a list of students or an empty array using the below edited codes

class Bootcamp{
    constructor(name,level,students){
        this.name = name;
        this.level = level;
        this.students = students || []; //if students undefined, set it to empty 
        //array
    }
}
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!