14 Useful JavaScript String Methods

14 Useful JavaScript String Methods

14 Useful JavaScript String Methods: indexOf, length, trim, concat, startsWith(), endsWith(), includes(), slice(), toUpperCase(), toLowerCase(), charAt(), split(), replace(), repeat(). The JavaScript string is an object that represents a sequence of characters. Knowing useful methods of a string is a still dream of many developers. So here I am, to explain some useful methods that will help you to manipulate string very easily.

The JavaScript string is an object that represents a sequence of characters. Knowing useful methods of a string is a still dream of many developers. So here I am, to explain some useful methods that will help you to manipulate string very easily.

  1. indexOf
  2. length
  3. trim
  4. concat
  5. startsWith()
  6. endsWith()
  7. includes()
  8. slice()
  9. toUpperCase()
  10. toLowerCase()
  11. charAt()
  12. split()
  13. replace()
  14. repeat()

Let’s Start 🙌


1. indexOf

The indexOf() method returns the index of (the position of) the first occurrence of a specified text.

const str ='I have Css, Wait I love CSS'
str.indexOf('Css');

// 7

2. length

The length property returns the length of a string.

const str = 'ILoveCss'
str.length;

// 8

3. trim

The trim() methods removes whitespace from both sides of the string

const str = '        ILoveCss'      ;
str.trim();

// ILoveCss

4. concat

concat() joins two or more strings. The concat() method can be used instead of the plus operator

const str = 'Ilove';
str.concat('Css')

// ILoveCss

5. startsWith()

Check if string starts with specified character(s).

const str = 'JavaScript is amazing';

  console.log(str.startsWith('JavaScript')); // true
  console.log(str.startsWith('Java')); // true
  console.log(str.startsWith('javascript')); // false
  // position is optional
  // if not specified, the default value is 0
  console.log(str.startsWith('Script', 4)); // true
  console.log(str.startsWith('SCRIPT', 4)); // false

6. endsWith()

Check if a string ends with specified character(s).

const str = 'JavaScript is amazing';

  // check if ends with 'amazing'
  console.log(str.endsWith('amazing')); // true
  console.log(str.endsWith('ing')); // true
  console.log(str.endsWith('Amazing')); // false
  // length is optional
  // if not specified, the default value is length of the string
  console.log(str.endsWith('is', 13)); // true
  console.log(str.endsWith('i', 13)); // false
  console.log(str.endsWith('s', 13)); // true

7. includes()

Check if string contains specified character(s).

const str = 'JavaScript is amazing';

  console.log(str.includes('Script')); // true
  console.log(str.includes('script')); // false
  console.log(str.includes(' ')); // true
  console.log(str.includes('array')); // false

8. slice()

Copy some part of string without modifying the original one.

const str = 'JavaScript is amazing';

  // Default start index is 0
  console.log(str.slice()); // 'JavaScript is amazing'
  // start copy at index 4
  console.log(str.slice(4)); // 'Script is amazing'
  // end copy at index 10(character at this index will not be included)
  console.log(str.slice(0, 10)); // 'JavaScript'

javascript programming web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.