Complete Javascript String methods with Examples





Hey guys in this post we will discuss String and their methods in javascript. The string is the most commonly used datatype in any programming language. It’s always good to know that the usage, methods, and examples of String.

Introduction


A Javascript string stores a series of characters like “B2 Tech – Web Dev Snippets”.

A string can be any text inside double or single quotes.

let str = "I love web development";
let str = "I love javascript";

How to create a string


There are 2 ways to create a string in Javascript

  • By string literal
  • By string object (using new keyword)

By string literal


The string literal is created using double quotes or single quotes

let str = "I love web development";

Let’s look at an example –

let str = "This is how we will create string literal";
console.log(str)

Output:

This is how we will create string literal

By string object (Using new keyword)


The syntax of creating a string object using a new keyword is given below

let str = new String("Using new keyword");

Here, new keyword is used to create instance of string

Let’s look at an example –

console.log(str);

Output:

Using new keyword

String methods


Following are the list of methods that are supported by String



  • charAt()
  • charCodeAt()
  • concat()
  • endsWith()
  • fromCharCode()
  • includes()
  • indexOf()
  • lastIndexOf()
  • localeCompare()
  • match()
  • repeat()
  • replace()
  • search()
  • slice()
  • split()
  • startsWith()
  • substr()
  • substring()
  • toLocaleLowerCase()
  • toLocaleUpperCase()
  • toLowerCase()
  • toString()
  • toUpperCase()
  • trim()
  • valueOf()

Examples


Let’s look at some of the examples of string methods

charAt(index)


It returns the character at the specified index in a string. If the specified index is greater than the length of the string then it will return "" string.

let test = "Hello world";
console.log(test.charAt(4)) // "o"
console.log(test.charAt(100)) // ""

concat(str)


Appends a string to the end of another string

let str = "java";
console.log(str.concat("script")); // "javascript"

indexOf(str)


returns the position of the first found occurrence of specified characters, otherwise returns -1

let str = "i love web development";
console.log(str.indexOf("web")) // 7
console.log(str.indexOf("java")) // -1

toLowerCase()


It converts all the characters to lower case for the given string.

let str = "JavaScript";
console.log(str.toLowerCase()) // "javascript"

toUpperCase()


It converts all the characters to upper case for the given string.

let str = "Javascript";
console.log(str.toUpperCase()) // "JAVASCRIPT"

slice(beginIndex, endIndex)


The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

let str = "abcdefgh";
console.log(str.slice(3, 7)); // "defg"

split()


The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.

let str = "I love web development";
console.log(str.split(" ")) // ["I", "love", "web", "development"]

charCodeAt()


The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

let str = "javascript";
console.log(str.charCodeAt(1)); // 97

Read more about string methods here




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply