New ES6 string methods

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
A variable declared with const can be mutable or immutable depending on the data type of the assigned value. However, a constant cannot be reassigned to a new value. The difference between assignment and immutability (mutability) is: The assignment ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

ES6 introduced new string methods such as startsWith(), endsWith(), includes(), padStart(), padEnd(), repeat().
To check if a string starts with a specified substring, use startsWith() .
It returns true or false
console.log("Atlanta, Ga".startsWith("Atl")) // true
console.log("Atlanta, Ga".startsWith("Ga")) // false
console.log("Atlanta, Ga".startsWith("Ga", 14)); // false
console.log("Atlanta, Ga".startsWith("Ga", 9)) //true
This method accepts an optional second parameter to specify the position in the string to start searching.
To check if a string ends with a specified substring, use endsWith() It returns true or false.
console.log("Atlanta, Ga".endsWith("Atl")) // false
console.log("Atlanta, Ga".endsWith("Ga")) // true
console.log("Atlanta, Ga".endsWith("Ga", 10)) //false
console.log("Atlanta, Ga".endsWith("Ga", 11)) //true
This method accepts an optional second parameter (length), which is the length of the string to consider for the search. If provided, only the characters within the specified length are considered. If not provided, the entire string is considered.
To check whether a string contains a substring, use includes(). It returns true or false.
console.log("Atlanta, Ga".includes("lan")); //true
console.log("Atlanta, Ga".includes("Lan")); //false
console.log("Atlanta, Ga".includes("lan", 10)); //false
console.log("Atlanta, Ga".includes("lan", 2)); //true
All of the above three methods perform a case-sensitive search.
The next two string methods padStart() and padEnd() accepts two arguments: the max length of the returned string, and the filler string:
To pad the start of a string, use padStart()
console.log("Alice".padStart(10, "Bob ")); // Bob Alice
console.log("def".padStart(6, "abc")); // abcdef
To pad the end of a string, use padEnd()
console.log("abcd".padEnd(7, "efg")) // abcdefg
If the filler is multiple chars and can’t evenly be added, it will be truncated:
console.log("Hello".padEnd(10, "all")); // Helloallal
If the max length is less than the original string’s length, the original string will be returned without any padding applied:
console.log("Hello, World".padEnd(10, "!!!")) // Hello, World
To repeat a string a given number of times, use repeat()
console.log("no".repeat(3)) // nonono
These are just a few of the new string methods introduced in ES6. They provide convenient ways to perform common string operations and manipulate strings in JavaScript. They are not only consistent but much easier to understand exactly what they’re doing at a glance.