Inside the String
Explore how to work with JavaScript strings by learning to initialize them in various ways and access individual characters using indexes. Understand string manipulation techniques like reversing strings, shifting characters, and checking for palindromes. This lesson guides you through hands-on exercises to practice these fundamental string operations, strengthening your programming skills in JavaScript.
Collection
A collection is a general term used to group multiple values into a single unit. We use collections all the time in the real world―a book is a collection of pages, a train is a collection of train cars, and a wardrobe is a collection of clothes.
String
A string is a collection of characters, mostly treated as a single unit. It is used to store text values like a name, address, message, etc.
Initializing the string
There are various ways to initialize a string in JavaScript. The following program demonstrates these methods:
There are a few new things that we need to understand in the code above. The following are the three ways of initializing a string variable:
Line 1: The text of
String1is enclosed in single quotes''.Line 4: The text of
String2is enclosed in double quotes"".Line 7: The text of
String3is enclosed in single backticks``.
Accessing the characters in a string
JavaScript allows us to access individual characters inside a string through an integer index number. To do this, we enclose the index number in square brackets after the string variable name, as shown in the following program:
The code above displays the individual characters of a string on separate lines, one by one. The first character is at the 0 index, and the index moves forward along the string in linear increments.
The same task can also be performed using loops as well. The previous program can be written in the following manner.
Practice for string manipulation
The following are a few example programs to practice using strings in JavaScript. Clicking the “Show Solution” button will display a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.
Remember The “Run” button becomes the “Stop” button when we run the code. We have to click the “Stop” button to edit the code. We can re-run the code by clicking on the “Run” button again, after editing. We can re-run to try different inputs of variables.
How to input: For input-based programs, once you run the program, it will require your input to execute. A pop-up box will appear to type the input in that box first to get the desired results. You must type the inputs one after the other for more than one input.
Print the characters backward
Write a program that displays the characters of a string backward, line by line.
Sample input
"Educative"
Sample output
e
v
i
t
a
c
u
d
E
Left shift the characters in a string
Write a program that shifts the characters of a string to the left and replaces the last position with a dot.
Sample input
"Educative"
Sample output
ducative.
Reverse the string
Write a program that stores the string into another string in reverse order.
Sample input
"Educative"
Sample output
evitacudE
Palindrome test
A palindrome is a sequence of characters that reads the same backward and forward. Write a program that checks if a string is a palindrome. The length of the string is restricted to five characters.
Sample input
"level"
Sample output
Palindrome