Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

Arrays & Strings

Arrays and Strings are important JavaScript data structures used to store, organize, and work with collections of data and text.

JavaScript Arrays & Strings Array 0 1 2 3 Multiple values in one collection String "JavaScript" Sequence of characters

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

1 Array

Used to store multiple values inside one collection.

2 Index

Array elements are accessed using indexes. The first index is 0.

3 String

Used to store and manipulate text.

4 Methods

JavaScript provides methods to add, remove, search, modify, and process Array and String data.

Arrays & Strings Flow

How JavaScript Handles Collections and Text
Data
Values / Text
→
Array / String
Store Data
→
Properties
length
→
Methods
Process Data
→
Result
Output

Example 1: Creating an Array

We can create an Array using square brackets [].

const courses = [
    "Java",
    ".NET",
    "Python",
    "JavaScript"
];

console.log(courses);
Output:
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]);
Output:
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);
Output:
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);
Output:
First Student: Rahul
Total Students: 3
Important Note:

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.