Tutorial
String Methods

String Properties & Methods

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ String Properties & Methods in JavaScript

Explaining

Properties are values that are associated with a string. For example, the length property of a string represents the number of characters in the string. Here is an example of how to use the length property:

var str = "Hello World!";
var n = str.length;

Methods are functions that can be called on a string to perform a specific action. For example, the toUpperCase() method returns a new string with all of the characters in uppercase. Here is an example of how to use the toUpperCase() method:

var str = "Hello World!";
var res = str.toUpperCase();
MethodDescription
charAt(index)Returns the character at the specified index.
charCodeAt(index)Returns the Unicode code point value of the character at the specified index.
concat(str1, str2, ...)Returns a new string that is the concatenation of the given strings.
endsWith(searchString, length)Returns true if the string ends with the specified search string, or false if it doesn't.
includes(searchString, position)Returns true if the string includes the specified search string, or false if it doesn't.
indexOf(searchString, position)Returns the index of the first occurrence of the specified search string, or -1 if it is not found.
lastIndexOf(searchString, position)Returns the index of the last occurrence of the specified search string, or -1 if it is not found.
localeCompare(compareString, locales, options)Compares the string to the specified compareString and returns a number indicating how they are sorted relative to each other.
match(regexp)Executes a search for a match in the string and returns an array of the results.
repeat(count)Returns a new string that is the original string repeated the specified number of times.
replace(searchValue, replaceValue)Returns a new string with some or all matches of a pattern replaced by a replacement.
search(regexp)Executes a search for a match in the string and returns the index of the first match, or -1 if no match is found.
slice(start, end)Extracts a section of the string and returns a new string.
split(separator, limit)Splits the string into an array of substrings based on the specified separator.
startsWith(searchString, position)Returns true if the string starts with the specified search string, or false if it doesn't.
substring(start, end)Extracts a section of the string and returns a new string.
toLowerCase()Returns a new string with all uppercase characters converted to lowercase.
toUpperCase()Returns a new string with all lowercase characters converted to uppercase.
trim()Removes leading and trailing white space from the string and returns a new string.
valueOf()Returns the primitive value of the string.
let str = 'Hello, world!';
 
// charAt(index)
console.log(str.charAt(0));  // Output: "H"
 
// charCodeAt(index)
console.log(str.charCodeAt(0));  // Output: 72
 
// concat(str1, str2, ...)
console.log(str.concat(' How are you?'));  // Output: "Hello, world! How are you?"
 
// endsWith(searchString, length)
console.log(str.endsWith('world!'));  // Output: true
 
// includes(searchString, position)
console.log(str.includes('world'));  // Output: true
 
// indexOf(searchString, position)
console.log(str.indexOf('world'));  // Output: 7
 
// lastIndexOf(searchString, position)
console.log(str.lastIndexOf('l'));  // Output: 9
 
// localeCompare(compareString, locales, options)
console.log(str.localeCompare('HELLO, WORLD!'));  // Output: 1
 
// match(regexp)
console.log(str.match(/l/g));  // Output: ["l", "l"]
 
// repeat(count)
console.log(str.repeat(3));  // Output: "Hello, world!Hello, world!Hello, world!"
 
// replace(searchValue, replaceValue)
console.log(str.replace(/l/g, 'L'));  // Output: "HeLLo, worLd!"
 
// search(regex)
console.log(str.search(/l/g));  // Output: 2
 
// slice(start, end)
console.log(str.slice(7, 12));  // Output: "world"
 
// split(separator, limit)
console.log(str.split(' '));  // Output: ["Hello,", "world!"]
 
// startsWith(searchString, position)
console.log(str.startsWith('Hello'));  // Output: true
 
// substring(start, end)
console.log(str.substring(7, 12));  // Output: "world"
 
// toLowerCase()
console.log(str.toLowerCase());  // Output: "hello, world!"
 
// toUpperCase()
console.log(str.toUpperCase());  // Output: "HELLO, WORLD!"
 
// trim()
console.log('   Hello, world!   '.trim());  // Output: "Hello, world!"
 
// valueOf()
console.log(str.valueOf());  // Output: "Hello, world!"
 

⚡ Playground

⭐ JavaScript String Objects

Explaining String Objects

You can also create strings using the new keyword. For example, the following code creates a string object:

let str = 'Hello, world!';
console.log(str);  // Output: "Hello, world!"
console.log(typeof str);  // Output: "string"
 
 
let str1 = new String('Hello, world!');
console.log(str1);  // Output: String {"Hello, world!"}
console.log(str1.typeof);  // Output: "object"

It is generally recommended to use primitive strings in JavaScript rather than String objects, because primitive strings are faster and more efficient.

More info:

Primitive strings are stored directly in the JavaScript engine's memory, while String objects are stored on the heap (a region of memory used for dynamic memory allocation). This means that primitive strings are generally faster and more efficient than String objects.

Additionally, primitive strings are immutable, meaning that their value cannot be changed once they are created. String objects, on the other hand, are mutable and can be modified.

For these reasons, it is generally recommended to use primitive strings in JavaScript rather than String objects. Using primitive strings can help improve the performance of your programs and make them more efficient.

JavaScript Type Conversion Methods

The following methods convert a string to another type:

  • Number() - Converts a string to a number. If the string cannot be converted to a number, it will return NaN.
let str = '123';
console.log(Number(str));  // Output: 123
  • parseInt() - Parses a string and returns an integer. If the first character cannot be converted to a number, it will return NaN.
let str = '123';
console.log(parseInt(str));  // Output: 123
  • parseFloat() - Parses a string and returns a floating point number. If the first character cannot be converted to a number, it will return NaN.
let str = '123.45';
console.log(parseFloat(str));  // Output: 123.45
  • String() - Converts a value to a string.
let num = 123;
console.log(String(num));  // Output: "123"