What is JavaScript String Reference ?

JavaScript string Reference includes JavaScript’s built-in string properties, methods, and behaviors that enable you to work with and manipulate text.

Strings represent text as sequences of characters. It provides a list of properties and methods to manipulate strings.  Any JavaScript code can be written in a script tag(between <script></script>) in the file of HTML or an external JavaScript file.

1. length

The length property returns the length of the given string.

Example:

let text = "Nilesh";
console.log("The Length of the Text Is: "text.length);

Output:

The Length of the Text Is: 6

2. charAt()

The charAt() gives the character at the given position(specified index). If you want to output the character at index position 10(m) then:

Example:

let text = "Hello from Nilesh";
console.log(text.charAt(10));

Output:

m

3. concat()

concat method joins two or more strings together.

Example:

let firstname = "Yadav";
let lastname = "Nilesh";
console.log("Full Name: "+firstname.concat(" ",lastname));

Output:

Full Name: Yadav Nilesh

The above code joins the strings of variables firstname and lastname and displays them together using concat().

4. charCodeAt()

It gives the Unicode of the character at a specified index. Unicode assigns a unique number to every character from every language and symbol set called a code point.

Example:

let a = "Hello coders";
console.log("The Unicode of e is: "+a.charcode(10));

Output:

The Unicode of e is: 101

5. includes()

It checks whether a string contains a certain word and returns true or false.

Example:

let string = "Hello Jatin";
console.log(string.includes("Jatin"));

Output:

true

6. indexOf()

It Returns the position of the first occurrence of a specified text in a string.

Example:

let string = "Hii Nilesh";
console.log(string.indexOf("Nilesh"));

Output:

4

This code gives the position of the first occurrence of string ‘Nilesh’ which is at index position 4.

7. lastIndexOf()

Returns the position of the last occurrence of a specified value in a string.

Example:

let string = "Nilesh Hii Nilesh";
console.log(string.lastIndexOf("Nilesh"));

Output:

13

8. slice()

Extracts a part of a string and returns a new string.

Example:

let string = "Web Devlopment is easy to learn";
console.log(string.slice(0,14));

Output:

Web Devlopment

9. substring()

It is similar to slice() but does not accept negative indices.

Example:

let wish = "Good Morning";
console.log(wish.substring(5,12));

Output:

Morning

10. replace()

Replace a specified value with another value in a string.

Example:

let string = "Good Morning"; 
console.log(string.replace("Morning", "Night"));

Output:

Good Night

11. toUpperCase()

toUpperCase() converts the string to upper case.

Example:

let string = "codespeedy"; 
console.log(string.toUpperCase());

Output:

CODESPEEDY

12.toLowerCase()

toLowerCase() converts the string to lowercase.

Example:

let string = "INTERNSHIP";
console.log(string.toLowerCase());

Output:

internship

13. startsWith()

It checks if a string starts with specified characters.

Example:

let string = "Nilesh is an intern in codespeedy";
console.log(string.startsWith("intern"));

Output:

false

14. endsWith()

It checks if a string ends with specified characters.

Example:

let string = "Nilesh is an intern in codespeedy";
console.log(string.endsWith("codespeedy"));

Output:

true

15. repeat()

This function repeats a given string several times and returns a new string.

Example:

let str = "Coders";
console.log(str.repeat(4));

Output:

CodersCodersCodersCoders

All these properties and methods can be used to perform operations on strings as per the user’s requirement.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top