Arrays & Strings
Arrays and Strings are important JavaScript data structures used to store, organize, and work with collections of data and text.
Arrays store multiple values, while Strings are used to work with text and characters.
Simple Explanation
Think of an Array like a box
containing multiple values.
For example, one Array can contain multiple course
names, numbers, student names, or other values.
A String is used to store text.
For example:
"JavaScript"
can be stored inside a String variable.
JavaScript provides many built-in properties and
methods to work with both Arrays and Strings.
What is an Array?
An Array is a collection of values stored in a single variable. Each value has an index, and Array indexing starts from 0.
const courses = [
"Java",
".NET",
"Python",
"JavaScript"
];
What is a String?
A String represents text in JavaScript. Strings can contain letters, numbers, spaces, and special characters.
const courseName = "JavaScript Full Stack";
Arrays & Strings at a Glance
Used to store multiple values inside one collection.
Array elements are accessed using indexes. The first index is 0.
Used to store and manipulate text.
JavaScript provides methods to add, remove, search, modify, and process Array and String data.
Arrays & Strings Flow
Values / Text
Store Data
length
Process Data
Output
Example 1: Creating an Array
We can create an Array using square brackets [].
const courses = [
"Java",
".NET",
"Python",
"JavaScript"
];
console.log(courses);
Java, .NET, Python, JavaScript
Example 2: Accessing Array Elements
Array elements can be accessed using their index. Remember that Array indexing starts from 0.
const courses = [
"Java",
".NET",
"Python",
"JavaScript"
];
console.log(courses[0]);
console.log(courses[2]);
Java
Python
Example 3: Working with a String
A String can store text, and JavaScript provides properties such as length to work with it.
const courseName = "JavaScript";
console.log(courseName);
console.log(courseName.length);
JavaScript
10
Example 4: Array and String Together
Arrays can store multiple Strings. This is commonly used when working with collections of names, courses, products, or other text values.
const students = [
"Rahul",
"Sneha",
"Amit"
];
console.log("First Student:", students[0]);
console.log("Total Students:", students.length);
First Student: Rahul
Total Students: 3
JavaScript Array indexes start from
0.
For example, in:
["Java", ".NET", "Python"]
Java has index 0,
.NET has index 1,
and Python has index 2.
What We Will Learn Next
In the next topics, we will learn Arrays and
Strings in more detail.
Arrays →
Array creation, indexing, updating, and
Array Methods.
Strings →
String creation, indexing, properties, and
String Methods.
Summary
Arrays are used to store multiple values in a single collection, while Strings are used to store and work with text. JavaScript provides indexes, properties, and built-in methods to access and process Array and String data. In the upcoming topics, we will explore Arrays, Array Methods, Strings, and String Methods in detail.