𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗖𝗼𝗻𝘀𝘁𝗿𝘂𝗰𝘁𝗼𝗿𝘀 𝗮𝗻𝗱 𝗔𝗿𝗿𝗮𝘆𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁
You need to understand how to build objects and manage lists in JavaScript. This guide covers constructors and arrays.
Constructors
A constructor creates multiple objects with the same structure. Use the "new" keyword to make a new instance.
Example: function Person(name = "Unknown", age = 0) { this.name = name; this.age = age; }
const person1 = new Person("Pranjal", 30); const person2 = new Person();
Person 1 holds the name Pranjal and age 30. Person 2 uses the default values.
Arrays
Arrays store lists of data. JavaScript arrays use zero-based indexing. This means the first item is at index 0.
Key rules: • Use .length to see the size of your list. • Use push to add items to the end. • Use pop to remove the last item. • Use unshift to add items to the start. • Use shift to remove the first item.
Practical Logic
You can use arrays to process data. For example, you can calculate grades from a list of marks.
- Loop through the array to find failing scores.
- Sum all numbers to get a total.
- Divide the total by the array length to find the average.
- Use if-else statements to assign a grade.
This logic helps you automate data tasks in your code.
Source: https://www.geeksforgeeks.org/javascript/javascript-arrays/ Source: https://www.geeksforgeeks.org/javascript/js-constructor-method/
Full post: https://dev.to/ezhil_abinayak_e38eec8fb/mastering-this-constructors-arrays-in-javascript-30o8
Optional learning community: https://t.me/GyaanSetuAi