Real data comes in groups: a list of marks, a line of text, a student with a name and an age. Arrays, strings and objects hold such data, and each has built-in tools.
Arrays
An array is an ordered list of values, written in square brackets. Its items are numbered from index 0, so marks[0] is the first item, and marks.length is the count. The method push adds an item at the end and pop removes the last. The methods unshift and shift do the same at the start. The method slice copies a part of an array, and splice adds or removes items in the original. The method map builds a new array by changing every item, and filter builds one with only the items that pass a test.
Definition: Array. An ordered list of values, numbered from index 0.
Table 3.10: Useful array methods
| Method | Effect | Changes original |
|---|---|---|
| push, pop | Add or remove at the end | Yes |
| shift, unshift | Remove or add at the start | Yes |
| slice | Copies a part | No |
| map | New array, each item changed | No |
| filter | New array, items passing a test | No |
Strings
A string has the property length and many methods. toUpperCase() and toLowerCase() change the case. indexOf("a") gives the position of the first match, or -1. slice(0, 3) copies a part, split(" ") breaks a string into an array, trim() removes spaces at both ends, and replace("a", "b") swaps text.
Objects
An object holds named values, called properties, inside braces: { name: "Asha", age: 20 }. A property is read with dot notation, as in student.name. An object can hold arrays, and an array can hold objects, which is how lists of records are stored.
Definition: Object. A collection of named values called properties, written with braces.
Date and Math
new Date() holds the current date and time, and methods such as getFullYear() and getMonth() read its parts. The Math object has Math.round(), Math.max(), Math.min() and Math.random(), which gives a number from 0 up to but not including 1.
Example 3.10: Filtering a list of students
const marks = [45, 82, 67];
console.log(marks.filter(m => m >= 50));
console.log(marks.map(m => m + 5));
marks.push(90);
console.log(marks.length, marks[3]);
const student = { name: "Asha", age: 20 };
console.log(student.name.toUpperCase(), student.age);
console.log("web design".split(" "));
console.log(Math.max(...marks), Math.round(7.5));
Output:
[ 82, 67 ]
[ 50, 87, 72 ]
4 90
ASHA 20
[ 'web', 'design' ]
90 8
Common mistakes
- Forgetting that array positions start at 0, so the last index is length minus 1.
- Expecting map and filter to change the original array, when they return a new one.
- Using dot notation on a property name that is stored in a variable.
- Comparing two arrays or objects with ===, which tests identity and not contents.
Key takeaways
- Arrays are numbered lists starting at index 0.
- map changes every item and filter keeps items that pass a test, and both return new arrays.
- Strings have methods such as toUpperCase, indexOf, slice and split.
- An object holds named properties, read with dot notation.
Practice questions
- What is the index of the first item of an array?
- What does filter return?
- How do you read the name property of an object called student?
Answers to the practice questions
-
- A new array holding only the items that pass the test.
- With student.name.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
