What is a String

A string is a chain of characters. You can use any letters, numbers, and special characters!

As said in 3.1, to declare a string, you use quotation marks:

let myString = "Happy birthday!!";

Strings have many properties. For example, we can return the length of a string (the number of characters in it) using the length property:

let text = "JavaScript";
console.log(text.length); // logs 10 to the console

String Concatenation

Concatenation in programming means to chain together values!

Concat Starts at index 0 and you can use both single quotes (‘) or double quotes (“) and it would still work

We can concatenate strings using the plus sign:

let firstName = "Aadi";
let lastName = "Bhat";
console.log(firstName + " " + lastName); // logs Aadi Bhat

We can also use commas if we want a space by default:

console.log(firstName, lastName);

Substrings

A substring is a smaller part of a string.

An easy way to take a substring of a string is using the substring property:

let alpha = "abcdefg";
console.log(alpha.substring(0, 3)); // logs abc
console.log(alpha.substring(3)) // logs defg

Making Functions for Strings

Let’s make some functions that take a string data type as a parameter!

This function will take in a string and return true or false if the string is a palindrome or not, respectively:

function isPalindrome(string) {
    // remove all spaces and convert string to lowercase
	cleanString = string.replace(" ", "").toLowerCase();

    // reverse the string
    reversedString = cleanString.split("").reverse().join("");
    
    // returns if the strings are equal
  	return cleanString == reversedString;
}

console.log(isPalindrome("abcDE DCBA")); // logs true
console.log(isPalindrome("apple juice")); // logs false

1. Which method is used to remove spaces from a string in JavaScript?

string.replace(" ", "")
string.split(" ")
string.replace(/\s+/g, "")

2. What does the .toLowerCase() method do?

Converts all characters in the string to lowercase
Reverses the string
Removes spaces from the string

3. In the palindrome function, what is the purpose of .reverse()?

It removes spaces from the string
It reverses the order of characters in an array
It converts the string to uppercase