JavaScript - Strings and Methods

0.0(0)
studied byStudied by 0 people
0.0(0)
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:40 PM on 2/3/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

35 Terms

1
New cards

An index

is the position of a character within a string, and it is zero-based. This means that the first character of a string has an index of 0, the second character has an index of 1, and so on. For example, in the string hello, the character h is at index 0, e is at index 1, l is at index 2, and so on.

2
New cards

Bracket notation

uses square brackets ([]) and the index of the character you want to access. Let's look at an example: let greeting = "hello"; console.log(greeting[1]); // "e".

3
New cards

length property

To get the last character of a string, you can use the length of the string minus one. The length property of a string tells you how many characters it contains, so to access the last character, you would subtract one from the length: In this case, the length of hello is 5, and the last character (o) is at index 4 which is 5 - 1. let greeting = "hello"; console.log (greeting[greeting.length - 1]); // "o"

4
New cards

Get multiple characters index concat

let greeting = "hello"; let firstTwo = greeting[0] + greeting[1]; // "he" console.log(firstTwo); In this example, we are concatenating the first and second characters using bracket notation to form the string he.

5
New cards

The \n escape

sequence tells JavaScript to insert a line break at that point, which results in the string being displayed across multiple lines. let poem = "Roses are red,\nViolets are blue,\nJavaScript is fun,\nAnd so are you."; console.log(poem);

6
New cards

Backslash

escape the inner quotes by placing a backslash () before them: let statement = "She said, \"Hello!\""; console.log(statement); // She said, "Hello!" The backslash tells JavaScript to treat the quotes as literal characters, so they appear correctly in the output.

7
New cards

Single backslash

You can also escape other special characters, such as the backslash itself (\), or single quotes within a string surrounded by single quotes (\'). Here's another example using single quotes: let quote = 'It\'s a beautiful day!'; console.log(quote); // It's a beautiful day! By escaping the single quote with \', JavaScript knows to include it as part of the string rather than ending the string early.

8
New cards

Template literals

make it easier to create strings that span multiple lines or include expressions (like variables or even JavaScript code) directly within the string. template literals are defined with backticks (`).

9
New cards

Template literal example

const name = "Alice"; const greeting = Hello, ${name}!; console.log(greeting);

10
New cards

String interpolation

allows you to embed variables and expressions inside a string. This is a significant improvement over the older method, where you would concatenate strings and variables using the + operator.

11
New cards

substring

is a sequence of characters that appears within a larger string. For example, in the string hello world, hello and world are substrings.

12
New cards

The indexOf() method

in JavaScript allows you to search for a substring within a string.

13
New cards

indexOf()

returns the index (or position) of the first occurrence of that substring. If the substring is not found, indexOf() returns -1, which indicates that the search was unsuccessful.

14
New cards

Two Arguments indexOf()

indexOf() method takes two arguments: the first is the substring you want to find within the larger string, and the second is an option starting position for the search. If you don't provide a starting position, the search will begin at the start of the string.

15
New cards

Argument

is a value you give to a function or method when you call it, enabling that function or method to perform its task using the specific information you provide. You will learn more about arguments in future lessons.

16
New cards

Example1 typeOf

You can also specify where to begin searching within the string by providing a second argument to indexOf(). Here's an example: let sentence = "JavaScript is awesome, and JavaScript is powerful!"; let position = sentence.indexOf("JavaScript", 10); console.log(position); // 27 In this case, the search for JavaScript begins after the 10th character, and so the second occurrence of JavaScript is found at index 27.

17
New cards

Example2 typeOf

the following would return -1 because the capital letter F is not found in the string freeCodeCamp. console.log("freeCodeCamp".indexOf("F")) // -1

18
New cards

The prompt()

method takes two arguments: The first one is the message which will appear inside the dialog box, typically prompting the user to enter information. And the second one is a default value which is optional and will fill the input field initially prompt(message, default);

19
New cards

What exactly does the prompt() method do?

It opens a dialog box that asks the user for some input, and then it returns the text entered by the user as a string.

20
New cards

Prompt example

const btn = document.getElementById("prompt-btn"); const output = document.getElementById("output"); btn.addEventListener("click", () => { const userName = prompt("What is your name?", "Guest"); output.textContent = "Hello, " + userName + "!"; });
21
New cards

Downsides of Prompt()

is useful for quick testing or small applications, it's generally avoided in modern, complex web applications due to its disruptive nature and inconsistent behavior across different browsers.

22
New cards

ASCII, short for American Standard Code for Information Interchange

is a character encoding standard used in computers to represent text. It assigns a numeric value to each character, which is universally recognized by machines.

23
New cards

The ASCII standard covers 128 characters including

Uppercase and lowercase English letters (A-Z, a-z). Numbers (0-9). Common punctuation marks and symbols (!, @, #, and so on). Control characters (such as newline and tab).

24
New cards

charCodeAt() method

This method is called on a string and returns the ASCII code of the character at a specified index. let letter = "A"; console.log(letter.charCodeAt(0)); // 65

25
New cards

fromCharCode() method

allows you to do the opposite: convert an ASCII code into its corresponding character. let char = String.fromCharCode(65); console.log(char); // A

26
New cards

The includes() method

is used to check if a string contains a specific substring. If the substring is found within the string, the method returns true otherwise, it returns false. let phrase = "JavaScript is awesome!"; let result = phrase.includes("Awesome"); console.log(result); // false. It is also case sensitive.

27
New cards

Includes() method example

For example, you might want to check if a user's input includes a specific word or character before performing some action. One way to achieve this is by using the includes() method.

28
New cards

The slice() method

allows you to extract a portion of a string and returns a new string, without modifying the original string. It takes two parameters: the starting index and the optional ending index. Let's look at a simple example of extracting part of a string: let message = “Hello, world!”; let greeting = message.slice(0, 5); console.log(greeting); // Hello

29
New cards

The toUpperCase() method

converts all the characters to uppercase letters and returns a new string with all uppercase characters. This is useful when you want to emphasize text or create consistency in the format of strings.

30
New cards

toUpperCase() example

let greeting = "Hello, World!"; let uppercaseGreeting = greeting.toUpperCase(); console.log(uppercaseGreeting); // "HELLO, WORLD!" The original string remains unchanged because toUpperCase() returns a new string, rather than modifying the original one.

31
New cards

The toLowerCase() method

converts all characters in a string to lowercase. This is helpful when you need to standardize input, such as when comparing user-provided text or making case-insensitive checks.

32
New cards

toLowerCase() example

= let shout = "I AM LEARNING JAVASCRIPT!"; let lowercaseShout = shout.toLowerCase(); console.log(lowercaseShout); // "i am learning javascript!"

33
New cards

The trim() method

is the most commonly used way to remove whitespace from both the beginning and the end of a string. let message = " Hello! "; console.log(message); // " Hello! "let trimmedMessage = message.trim(); console.log(trimmedMessage); // "Hello!"

34
New cards

trimStart()

removes whitespace from the beginning (or start) of the string.

35
New cards

trimEnd()

removes whitespace from the end of the string.