Learn JavaScript
Learn JavaScript
Strings
TechGlobal School
Swipe
01/18
JavaScript Strings for
Beginners
Strings are useful for holding data that can be represented in
text form.
A string is zero or more characters written inside quotes.
We could use either double or single quotes to create a string
(Before ES6).
We can store strings in backticks as well (After ES6).
TechGlobal School
02/18
Template Literals (ES6)
Template literals, introduced in ES6, provide a convenient
way to work with strings in JavaScript, allowing embedded
expressions and multiline strings.
TechGlobal School
03/18
Template Literals (ES6)
TechGlobal School
04/18
Template Literals (ES6)
TechGlobal School
05/18
String length property
TechGlobal School
06/18
String access property
The access property allows you to access individual
characters in a string using indexes.
Zero-Based Indexing: The index of the first character in a
string is 0, the second character is at index 1, and so on.
Accessing the Last Character: The last character in a string
can be accessed using str[str.length - 1].
TechGlobal School
07/18
BONUS: Escape
Sequences
Escape sequences are special characters or combinations of
characters that are used to represent certain characters that
have special meanings or cannot be typed directly. Here are
some commonly used escape sequences in JavaScript:
TechGlobal School
08/18
Most common
String methods
TechGlobal School
09/18
charAt() method
The charAt() method allows you to access individual
characters in a string using indexes.
Zero-Based Indexing: The index of the first character in a
string is 0, the second character is at index 1, and so on.
Accessing the Last Character: The last character in a string
can be accessed using str.charAt(str.length - 1).
Empty Case: Accessing an index beyond the string's length
returns an empty string which is the difference between the
string access property and the charAt() method.
TechGlobal School
10/18
concat() method
The concat() method in JavaScript is used to concatenate
(join together) two or more strings, creating a new string
without modifying the original strings.
TechGlobal School
11/18
endsWith() and
startsWith() methods
The endsWith() method in JavaScript is used to check if a
string ends with a specified substring.
The startsWith() method in JavaScript is used to check if a
string starts with a specified substring.
Both functions return a boolean (true or false) based on
whether the string ends/starts with the specified substring.
Both are case-sensitive; they consider the exact characters
provided in the substring when comparing.
TechGlobal School
12/18
includes() method
The includes() method in JavaScript is used to check
whether a string contains a specific substring.
It returns a boolean (true or false) based on whether the
string includes the specified substring.
This method is case-sensitive; it matches the exact
characters provided in the substring when comparing.
If the position parameter is provided, it starts the search
from that index in the string.
TechGlobal School
indexOf() and 13/18
lastIndexOf() methods
Both indexOf() and lastIndexOf() methods in JavaScript are
used to find the position of a substring within a string.
The indexOf() method searches the string for a specified
substring and returns the position of the first occurrence of
the substring.
The lastIndexOf() method works like indexOf() but searches
for the last occurrence of a specified substring in a string.
They both return -1 if the substring is not found.
Both methods are case-sensitive.
If fromIndex is provided, the search starts from that index
for indexOf() and backward from that index for
lastIndexOf().
TechGlobal School
replace() and 14/18
replaceAll() methods
Both replace() and replaceAll() methods in JavaScript are
used to replace occurrences of a specified substring or
pattern within a string with another string or value.
TechGlobal School
slice() and 15/18
substring() methods
Both slice() and substring() methods in JavaScript are used
to extract a part of a string and return a new string without
modifying the original string.
They take one or two parameters: the start index and the
optional end index (up to but not including the end index).
They extract a substring from the start index to the end of
the string if the end index is omitted.
The slice() method allows negative indices to reference
characters from the end of the string. A negative index
counts backward from the end of the string.
TechGlobal School
split() method 16/18
TechGlobal School
toLowerCase() and 17/18
toUpperCase() methods
TechGlobal School
trim() method 18/18
TechGlobal School
Follow Us !
techglobal.school
techglobalschool
techglobalschool
techglobalschl
techglobalschool
www.techglobalschool.com