4. String Methods & Conditional Statements
Table of Contents:

Summary
Strings are sequences of characters enclosed in quotes. You can access individual characters using bracket notation and check their length with
.length. Strings are immutable, meaning their characters cannot be changed directly.String methods like
includes,startsWith,endsWith,indexOf,slice,toUpperCase, andreplaceallow you to search, analyze, and manipulate string data.Conditional statements (
if,else if,else) let you control the flow of your program based on data values. The order of conditions matters, and only the first true condition in a chain will execute.Guard clauses are single
ifstatements that return early from a function, simplifying conditional logic.Ternary operators provide a concise way to choose between two values based on a condition.
Strings
A string is a sequence of characters between single or double quotations.
Escaping Characters
If you want to use quotation marks in your string, you have to "escape" them by putting a \ in front.
There are many other characters that you can use the escape character (\) such as the "tab" character (\t) and the "newline" character (\n):
String Interpolation
When used within backticks, an expression can be interpolated into a string for easy formatting:
String Indexes and Bracket Notation
Each character in a string, including spaces, has an index — a numbered position starting at 0.
We can get a single character from a string using Bracket Notation and the index: string[index]
It works on variables too!
If you use bracket notation and there is no character at the given index, undefined will be returned:
Strings are Immutable
Strings are "immutable" (unable to be mutated). In other words, you cannot use bracket notation to change characters of a string.
If you try, no error will be thrown, nothing will happen.
String Length
Every string also has a length property that is accessed using dot notation: string.length
It tells us the number of characters in a string (including spaces). When combined with bracket notation, it can be useful for counting indexes backwards from the end of a string.
String Methods
A method is a function that is attached to a value. Often, methods are used to manipulate the value they are attached to.
Methods are invoked using dot notation:
value.method()
First, let's look at some "read only" string methods that return a boolean: includes, startsWith, endsWith,
The methods indexOf, lastIndexOf return a number representing the index of a particular character we're looking for:
When combined, these methods can be used in clever ways!
The following methods return a copy of a string modified in some way: slice, toUpperCase, toLowerCase, repeat, replace, replaceAll
Conditional Statements
Conditional statements allow us to change the behavior of our programs based on the current values of our data.

if/else if/else statements
if/else if/else statementsTo define the possible decisions that our program can make, we use if, else if, and else statements:
ifandelse ifstatements require a boolean condition to evaluate totruein order to runThere can be many
else ifstatements, or none at all.elsestatements do not require a condition and will run only when none of the other options do.
Order Matters
Only the first condition that is true will be executed. This means we have to be careful with the order in which we write our conditional statements.
Q: What happens if we rearrange the conditional statements like so?
Only if statements
if statementsIn an if/else if/else chain of conditional statements, only one statement will be executed. But what if we replace all of the else if and else statements with other if statements?
Guard Clauses
When working with a function that changes the value returned based on a condition, we can avoid using conditional chains and only use if statements called "guard clauses".
A guard clause is an if statement that returns before subsequent return statements have a chance to be executed.
Use Ternary Operators To Simplify Conditionals
ifandelsestatements let you choose between one of two code blocks to execute.The ternary operator
condition ? valA : valBis used to choose between one of two values.
Last updated