this Keyword in Javascript
In JavaScript, the this keyword refers to the object that is currently executing or invoking the function.It is used to access the properties and methods of that object without using object name repeatedly.
Constructor
A constructor is a special function used to create and intialize objects in js. We use it we need to create multiple objects with the same properties and methods. Instead of writing the same object structure again and again, we can define it once inside a constructor and create multiple objects from it. It is considered good practice to name constructor functions with an upper-case first letter.When we create a object constructor called automatically.
For Example, If we need to create many student object with properties like name, age, we can use a constructor instead of creating each object separately.
function Person(name = "Unknown", age = 0) {
this.name = name;
this.age = age;
}
const person1 = new Person("Pranjal", 30);
const person2 = new Person("Amar");
const person3 = new Person();
console.log(person1);
console.log(person2);
console.log(person3);
//
{"name":"Pranjal","age":30}
{"name":"Amar","age":0}
{"name":"Unknown","age":0}
Array
In JavaScript, an array is a global object used to store ordered collections of data. JavaScript arrays are dynamic (they can automatically change size) and heterogeneous (they can hold a mix of different data types like numbers, strings, and objects at the same time).
Creating an Array
The easiest and most common way to create an array is by using the array literal syntax with square brackets []:
// An empty array
const empty = [];
// An array containing strings, numbers, and
booleans
const mix = ["Apple", 42, true];
Zero-Indexed: The first item is always at index 0, the second is at 1, and so on.
The Length Property: Tracking array size is handled automatically via .length.
Essential Array Operations & Methods
1. Accessing and Modifying Elements
You can read or change items by referencing their index inside square brackets.
const tools = ["HTML", "CSS", "JS"];
console.log(tools[0]); // "HTML"
tools[1] = "Sass"; // Updates the item
console.log(tools); // ["HTML", "Sass", "JS"]
console.log(tools[1]); // "Sass"
2. Adding and Removing Elements
JavaScript provides built-in methods to easily modify items from the start or end of an array.
push(): Adds an item to the end.
pop(): Removes the last item.
unshift(): Adds an item to the beginning.
shift(): Removes the first item.
let fruits = ["Banana", "Orange"];
// Add to the end
fruits.push("Apple");
console.log(fruits); // Output: ["Banana", "Orange", "Apple"]
// Remove from the end
fruits.pop();
console.log(fruits); // Output: ["Banana", "Orange"]
// Add to the beginning
fruits.unshift("Mango");
console.log(fruits); // Output: ["Mango", "Banana", "Orange"]
// Remove from the beginning
fruits.shift();
console.log(fruits); // Output: ["Banana", "Orange"]
To calculate the total, average, grade, and pass/fail status from a JavaScript array.
const marks=[98,99,67,78,90];
const marks =;
console.log(marks.length);
let failCount = checkFailcount(marks);
console.log(failCount);
if (failCount > 0) {
console.log("no grade");
} else {
let total = findTotal(marks);
let avg = findAvg(total, marks.length);
let grade = findGrade(avg);
console.log("Total:", total, "Avg:", avg, "Grade:", grade);
}
function checkFailcount(studMarks) {
let i = 0;
let count = 0;
while (i < studMarks.length) {
if (studMarks[i] < 35) {
count++;
}
i++;
}
return count;
}
function findTotal(studMarks) {
let i = 0;
let total = 0;
while (i < studMarks.length) {
total = total + studMarks[i];
i++;
}
return total;
}
function findAvg(total, countofsub) {
return total / countofsub;
}
function findGrade(avg) {
if (avg >= 90) { return 'A'; }
else if (avg >= 70) { return 'B'; }
else { return 'C'; }
}
//5
0
Total: 432 Avg: 86.4 Grade: B
References
1.https://www.geeksforgeeks.org/javascript/javascript-arrays/
2.https://www.geeksforgeeks.org/javascript/js-constructor-method/
Top comments (0)