𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗖𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀 𝗮𝗻𝗱 𝗔𝗿𝗿𝗮𝘆𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁

JavaScript relies on constructors and arrays to manage data. Understanding these basics helps you write cleaner code.

Constructors

Constructors act as blueprints for objects. Use the new keyword to create instances.

Example:

function Person(name = "Unknown", age = 0) { this.name = name; this.age = age; }

const person1 = new Person("Pranjal", 30); const person2 = new Person("Amar");

The "this" keyword refers to the object being created.

Arrays

Arrays store lists of data. They use zero-based indexing. This means the first item sits at index 0.

Key Array Rules: • Use .length to find the size of your list. • Use .push() to add items to the end. • Use .pop() to remove items from the end. • Use .unshift() to add items to the start. • Use .shift() to remove items from the start.

Practical Logic

You use arrays to process numbers. You can loop through marks to find a total or an average.

If you want to check student grades, follow these steps:

  1. Count how many scores fall below the passing mark.
  2. Sum the total scores using a loop.
  3. Divide the total by the number of items to get the average.
  4. Assign a grade based on that average.

This logic keeps your data organized and your functions predictable.

Source: https://dev.to/ezhil_abinayak_e38eec8fb/mastering-this-constructors-arrays-in-javascript-30o8