0% found this document useful (0 votes)
11 views

Learn JavaScript

This document discusses JavaScript strings and common string methods. It covers how to create, manipulate, and work with strings in JavaScript. Template literals, escape sequences, and many string methods like charAt(), concat(), includes(), and trim() are explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Learn JavaScript

This document discusses JavaScript strings and common string methods. It covers how to create, manipulate, and work with strings in JavaScript. Template literals, escape sequences, and many string methods like charAt(), concat(), includes(), and trim() are explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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)

String Concatenation: Strings can be concatenated using


the + operator or template literals.
String Interpolation: Template literals use backticks (`) to
enclose strings and allow embedded expressions using ${}.
This feature enables easier string interpolation.

TechGlobal School
04/18
Template Literals (ES6)

Expression Evaluation: Expressions within ${} in template


literals are evaluated, allowing variables, functions, or any
valid JavaScript expressions.

TechGlobal School
05/18
String length property

The length property is used to find the length of a string.

length of a string = total number of characters including the


whitespaces.

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].

Undefined Case: Accessing an index beyond the string's


length returns undefined.

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:

\' represents a single quote. \\ represents a backslash.


\" represents a double quote. \n represents a new line.
\t represents a tab.

TechGlobal School
08/18
Most common
String methods

JavaScript provides numerous string methods for


manipulating and working with strings.

A comprehensive list of commonly used string


methods:
charAt() slice()
concat() split()
endsWith() startsWith()
includes() substring()
indexOf() toLowerCase()
lastIndexOf() toUpperCase()
replace() trim()
replaceAll()

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.

If no arguments are provided, concat() returns a copy of the


original string.

The + operator can also be used for string concatenation.

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.

They return a new string with the replacements. The


original string remains unchanged.

The main difference is that replace() replaces only the first


occurrence, whereas replaceAll() replaces all occurrences
within the string.
They return a new string that is identical to the original
string when the specified search value is not found within
the original string.

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

The split() method in JavaScript is used to split a string into


an array of substrings based on a specified separator and
returns the resulting array.
This method helps break a string into parts based on
characters, words, or patterns.

TechGlobal School
toLowerCase() and 17/18

toUpperCase() methods

Both toLowerCase() and toUpperCase() methods in


JavaScript are used to convert the case of characters within
a string.
The toLowerCase() method converts all the characters in a
string to lowercase while the toUpperCase() method
converts all characters in a string to uppercase.
The space, special characters, and digits remain unchanged.

TechGlobal School
trim() method 18/18

The trim() method in JavaScript is used to remove


whitespace from both ends (beginning and end) of a string.

The whitespace includes spaces, tabs, and newline characters.

The trimStart() method in JavaScript removes whitespace


characters (spaces, tabs, etc.) from the beginning (start) of a
string.
The trimEnd() method removes trailing whitespace (spaces,
tabs, etc.) from the end of a string.
These methods do not modify the original string; instead,
they generate and return a new string.

TechGlobal School
Follow Us !
techglobal.school

techglobalschool

techglobalschool

techglobalschl

techglobalschool

www.techglobalschool.com

You might also like