A Code Repo for JavaScript
Strings are a fundamental data type in JavaScript, representing sequences of characters. Manipulating strings is a common task in web development, and JavaScript provides a plethora of built-in methods to help with this. In this short guide, we'll introduce you to some of the most commonly used string methods.
The length property is not a method, but it's essential for every JavaScript developer to know. It returns the number of characters in a string.
const greeting = "Hello, World!";
const length = greeting.length; // length will be 13
These methods allow you to change the case of the characters in a string.
const message = "Hello, World!";
const upperCaseMessage = message.toUpperCase(); // "HELLO, WORLD!"
const lowerCaseMessage = message.toLowerCase(); // "hello, world!"
The charAt() method returns the character at a specified index.
const str = "Hello";
const char = str.charAt(1); // "e"
This method concatenates two or more strings and returns a new string.
const firstName = "John";
const lastName = "Doe";
const fullName = firstName.concat(" ", lastName); // "John Doe"
slice() extracts a portion of a string and returns it as a new string. The start index is inclusive, and the end index is exclusive.
const str = "Hello, World!";
const sliced = str.slice(0, 5); // "Hello"
indexOf() returns the index of the first occurrence of a specified substring. lastIndexOf() does the same, but starts searching from the end of the string.
const sentence = "To be or not to be, that is the question.";
const index = sentence.indexOf("be"); // 3
const lastIndex = sentence.lastIndexOf("be"); // 16
replace() searches a string for a specified value and replaces it with another value.
const str = "Hello, World!";
const newStr = str.replace("World", "Universe"); // "Hello, Universe!"
trim() removes whitespace from both ends of a string.
const str = " Hello, World! ";
const trimmed = str.trim(); // "Hello, World!"
In JavaScript, there are several array methods that manipulate the original array. These methods directly modify the array they are called on. Here are some commonly used array methods that perform in-place modifications:
Example:
Copy code
const myArray = [1, 2, 3];
myArray.push(4); // myArray is now [1, 2, 3, 4]
Example:
Copy code
const myArray = [1, 2, 3];
const poppedElement = myArray.pop(); // myArray is now [1, 2], poppedElement is 3
Example:
Copy code
const myArray = [1, 2, 3];
const shiftedElement = myArray.shift(); // myArray is now [2, 3], shiftedElement is 1
unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.
Example:
Copy code
const myArray = [2, 3];
myArray.unshift(1); // myArray is now [1, 2, 3]
splice(): Changes the contents of an array by removing, replacing, or adding elements. It can be used to add, remove, or replace elements at any position.
Example:
Copy code
const myArray = [1, 2, 3, 4];
myArray.splice(1, 2); // myArray is now [1, 4]
Example:
Copy code
const myArray = [1, 2, 3, 4];
myArray.reverse(); // myArray is now [4, 3, 2, 1]
Example:
Copy code
const myArray = [3, 1, 4, 1, 5, 9, 2, 6];
myArray.sort(); // myArray is now [1, 1, 2, 3, 4, 5, 6, 9]