Master JavaScript Programming From Scratch

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

Strings

Strings are used to store and work with text in JavaScript. They can contain letters, numbers, symbols, spaces, and other characters.

What is a String?

A string is a sequence of characters used to represent text in JavaScript.

Strings are written inside single quotes, double quotes, or backticks.

String Example

A string can contain a name, course name, city, message, or any other text.

Example: "JavaScript"

JavaScript String characters and indexes

A JavaScript string contains characters arranged using zero-based index positions.

Important String Concepts

String Value
Store Text

Strings are commonly used to store text values such as names and messages.

Index
Access Characters

Each character in a string has an index. The first character starts at index 0.

length
Count Characters

The length property tells us the length of a string.

+
Combine Strings

The plus operator can be used to combine two or more strings.

String Indexing

Characters and Their Index Positions
J
0
a
1
v
2
a
3
S
4
c
5
r
6
i
7
p
8
t
9

JavaScript uses zero-based indexing, so the first character is at index 0.

Example 1: Creating a String

We can create a string by assigning text inside quotation marks to a variable.

let course = "JavaScript";

console.log(course);
Output: JavaScript

Example 2: Accessing a Character

We can access an individual character of a string using its index position.

let course = "JavaScript";

console.log(course[0]);
console.log(course[4]);
Output: J
S

Example 3: Finding String Length

The length property returns the length of a string.

let course = "JavaScript";

console.log(course.length);
Output: 10

Example 4: Combining Strings

The + operator can be used to combine two strings.

let firstName = "CIIT";
let course = "JavaScript";

let message = firstName + " " + course;

console.log(message);
Output: CIIT JavaScript

Summary

Strings are used to represent text in JavaScript. Characters can be accessed using index positions, the length property can be used to find string length, and the + operator can be used to combine strings.