JavaScript is a powerful and flexible language, and two of its most important concepts are constructor functions and arrays.
🔷 What is a Constructor Function?
A constructor function is used to create and initialize multiple objects with the same structure. Instead of writing the same object again and again, you define a template and reuse it.
Think of it like a blueprint for creating objects.
Example:
const student1={
name:"arun",
age:22
}
const student2={
name:"raghu",
age:23
}
const student3={
name:"sandy",
age:21
}
const student4={
name:"vicky",
age:22
}
console.log(student1.name,student1.age)
console.log(student2.name,student2.age)
console.log(student3.name,student3.age)
console.log(student4.name,student4.age)
🔹It's normal object creation for a student details.
🔹This method is enough for one or two person but if we have more than 10 or 20 person we can't use this method.
🔹To overcom this method we are useing constructor function.
example:
function Student(name, age) {
this.name = name;
this.age = age;
}
This function doesn’t create an object yet it just defines how an object should look.
Explanation of this
🔹this refers to the object that is currently being created or operated on.
🔹In the context of a constructor function, this points to the newly created instance of that object.
🔹Creating Objects Using a Constructor
To create an object, we use the new keyword:
let s1 = new Student("Arun", 25);
let s2 = new Student("Bala", 23);
Now you have two separate objects created from the same blueprint.
Screenshot of the program:
What is an Array?
An array is a special type of object used to store multiple values in a single variable.
let numbers = [1, 2, 3];
Basic Array Methods
Arrays come with built-in methods that make data manipulation easy.
Array length
Array toString()
Array at()
Array join()
Array pop()
Array push()
Array shift()
Array unshift()
Array isArray()
Array delete()
Array concat()
Array copyWithin()
Array flat()
Array slice()
Array splice()
Array toSpliced()

Top comments (0)